From e3322525f86c7a54cd2ab9b66bf35e8dd1c48dc2 Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Wed, 7 Mar 2018 15:39:50 -0800 Subject: [PATCH] Adds one fake conditional branch for a sequence of loads --- lib/CodeGen/CodeGenPrepare.cpp | 38 ++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index f9b3b2d8ca0..96fa10bbb8e 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -328,7 +328,15 @@ Value* createCast(IRBuilder& Builder, Value* DepVal, Instruction::CastOps CastOp = Instruction::BitCast; switch (DepVal->getType()->getTypeID()) { case Type::IntegerTyID: { - CastOp = Instruction::SExt; + assert(TargetIntegerType->getTypeID() == Type::IntegerTyID); + auto* FromType = dyn_cast(DepVal->getType()); + auto* ToType = dyn_cast(TargetIntegerType); + assert(FromType && ToType); + if (FromType->getBitWidth() <= ToType->getBitWidth()) { + CastOp = Instruction::ZExt; + } else { + CastOp = Instruction::Trunc; + } break; } case Type::FloatTyID: @@ -729,6 +737,28 @@ Instruction* findFirstStoreCondBranchInst(LoadInst* LI, Vector* ChainedBB) { } } +// XXX-update: Find the next node of the last relaxed load from 'FromInst' to +// 'ToInst'. If none, return 'ToInst'. +Instruction* findLastLoadNext(Instruction* FromInst, Instruction* ToInst) { + if (FromInst == ToInst) { + return ToInst; + } + Instruction* LastLoad = ToInst; + auto* BB = FromInst->getParent(); + auto BE = BB->end(); + auto BBI = BasicBlock::iterator(FromInst); + BBI++; + for (; BBI != BE && &*BBI != ToInst; BBI++) { + auto* LI = dyn_cast(&*BBI); + if (LI == nullptr || !LI->isAtomic() || LI->getOrdering() != Monotonic) { + continue; + } + LastLoad = LI; + LastLoad = LastLoad->getNextNode(); + } + return LastLoad; +} + // XXX-comment: Returns whether the code has been changed. bool taintMonotonicLoads(const SmallVector& MonotonicLoadInsts) { bool Changed = false; @@ -825,10 +855,10 @@ void TaintRelaxedLoads(Instruction* UsageInst, Instruction* InsertPoint) { // Now we have a previously added fake cond branch. auto* Op00 = Op0->getOperand(0); IRBuilder Builder(CmpInst); - if (UsageInst->getType() == TargetIntegerType) { + if (Op00->getType() == UsageInst->getType()) { AndTarget = UsageInst; } else { - AndTarget = createCast(Builder, UsageInst, TargetIntegerType); + AndTarget = createCast(Builder, UsageInst, Op00->getType()); } AndTarget = Builder.CreateAnd(Op00, AndTarget); auto* AndZero = dyn_cast(Builder.CreateAnd( @@ -841,7 +871,7 @@ void TaintRelaxedLoads(Instruction* UsageInst, Instruction* InsertPoint) { } IRBuilder Builder(InsertPoint); - if (UsageInst->getType() == TargetIntegerType) { + if (IntegerType::classof(UsageInst->getType())) { AndTarget = UsageInst; } else { AndTarget = createCast(Builder, UsageInst, TargetIntegerType); -- 2.34.1