Adds one fake conditional branch for a sequence of loads
[oota-llvm.git] / lib / CodeGen / CodeGenPrepare.cpp
index f9b3b2d8ca07080576ef99d8f772943616a9c40e..96fa10bbb8eaddcf96c6ac91b92e9acfda2a91c2 100644 (file)
@@ -328,7 +328,15 @@ Value* createCast(IRBuilder<true, NoFolder>& 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<IntegerType>(DepVal->getType());
+      auto* ToType = dyn_cast<IntegerType>(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<LoadInst>(&*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<LoadInst*, 1>& 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<true, NoFolder> 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<Instruction>(Builder.CreateAnd(
@@ -841,7 +871,7 @@ void TaintRelaxedLoads(Instruction* UsageInst, Instruction* InsertPoint) {
   }
 
   IRBuilder<true, NoFolder> Builder(InsertPoint);
-  if (UsageInst->getType() == TargetIntegerType) {
+  if (IntegerType::classof(UsageInst->getType())) {
     AndTarget = UsageInst;
   } else {
     AndTarget = createCast(Builder, UsageInst, TargetIntegerType);