Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | d2a251f | 2009-07-17 21:33:58 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // CallSite Class |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 32 | User::op_iterator CallSite::getCallee() const { |
| 33 | Instruction *II(getInstruction()); |
| 34 | return isCall() |
Gabor Greif | 638c823 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 35 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 36 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // TerminatorInst Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | // Out of line virtual method, so the vtable, etc has a home. |
| 44 | TerminatorInst::~TerminatorInst() { |
| 45 | } |
| 46 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // UnaryInstruction Class |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 51 | // Out of line virtual method, so the vtable, etc has a home. |
| 52 | UnaryInstruction::~UnaryInstruction() { |
| 53 | } |
| 54 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 56 | // SelectInst Class |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 60 | /// for a select operation, otherwise return null. |
| 61 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 62 | if (Op1->getType() != Op2->getType()) |
| 63 | return "both values to select must have same type"; |
| 64 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 65 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 66 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 67 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 68 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 69 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 70 | if (ET == 0) |
| 71 | return "selected values for vector select must be vectors"; |
| 72 | if (ET->getNumElements() != VT->getNumElements()) |
| 73 | return "vector select requires selected vectors to have " |
| 74 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 75 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 76 | return "select condition must be i1 or <n x i1>"; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 83 | // PHINode Class |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | PHINode::PHINode(const PHINode &PN) |
| 87 | : Instruction(PN.getType(), Instruction::PHI, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 88 | allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 89 | ReservedSpace(PN.getNumOperands()) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 90 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 91 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 92 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 95 | PHINode::~PHINode() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 96 | dropHungoffUses(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 99 | Use *PHINode::allocHungoffUses(unsigned N) const { |
| 100 | // Allocate the array of Uses of the incoming values, followed by a pointer |
| 101 | // (with bottom bit set) to the User, followed by the array of pointers to |
| 102 | // the incoming basic blocks. |
| 103 | size_t size = N * sizeof(Use) + sizeof(Use::UserRef) |
| 104 | + N * sizeof(BasicBlock*); |
| 105 | Use *Begin = static_cast<Use*>(::operator new(size)); |
| 106 | Use *End = Begin + N; |
| 107 | (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1); |
| 108 | return Use::initTags(Begin, End); |
| 109 | } |
| 110 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 111 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 112 | // predecessor basic block is deleted. |
| 113 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 114 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 115 | |
| 116 | // Move everything after this operand down. |
| 117 | // |
| 118 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 119 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 120 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 121 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 122 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 123 | |
| 124 | // Nuke the last value. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 125 | Op<-1>().set(0); |
| 126 | --NumOperands; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 127 | |
| 128 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 129 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 130 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 131 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 132 | eraseFromParent(); |
| 133 | } |
| 134 | return Removed; |
| 135 | } |
| 136 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 137 | /// growOperands - grow operands - This grows the operand list in response |
| 138 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 139 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 140 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 141 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 142 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 143 | unsigned NumOps = e + e / 2; |
| 144 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 145 | |
| 146 | Use *OldOps = op_begin(); |
| 147 | BasicBlock **OldBlocks = block_begin(); |
Jay Foad | e03c05c | 2011-06-20 14:38:01 +0000 | [diff] [blame] | 148 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | ReservedSpace = NumOps; |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 150 | OperandList = allocHungoffUses(ReservedSpace); |
| 151 | |
| 152 | std::copy(OldOps, OldOps + e, op_begin()); |
| 153 | std::copy(OldBlocks, OldBlocks + e, block_begin()); |
| 154 | |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 155 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 158 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 159 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 160 | Value *PHINode::hasConstantValue() const { |
| 161 | // Exploit the fact that phi nodes always have at least one entry. |
| 162 | Value *ConstantValue = getIncomingValue(0); |
| 163 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 164 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 165 | if (ConstantValue != this) |
| 166 | return 0; // Incoming values not all the same. |
| 167 | // The case where the first value is this PHI. |
| 168 | ConstantValue = getIncomingValue(i); |
| 169 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 170 | if (ConstantValue == this) |
| 171 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 172 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 175 | //===----------------------------------------------------------------------===// |
| 176 | // LandingPadInst Implementation |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | |
| 179 | LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, |
| 180 | unsigned NumReservedValues, const Twine &NameStr, |
| 181 | Instruction *InsertBefore) |
| 182 | : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) { |
| 183 | init(PersonalityFn, 1 + NumReservedValues, NameStr); |
| 184 | } |
| 185 | |
| 186 | LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, |
| 187 | unsigned NumReservedValues, const Twine &NameStr, |
| 188 | BasicBlock *InsertAtEnd) |
| 189 | : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) { |
| 190 | init(PersonalityFn, 1 + NumReservedValues, NameStr); |
| 191 | } |
| 192 | |
| 193 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
| 194 | : Instruction(LP.getType(), Instruction::LandingPad, |
| 195 | allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()), |
| 196 | ReservedSpace(LP.getNumOperands()) { |
| 197 | Use *OL = OperandList, *InOL = LP.OperandList; |
| 198 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 199 | OL[I] = InOL[I]; |
| 200 | |
| 201 | setCleanup(LP.isCleanup()); |
| 202 | } |
| 203 | |
| 204 | LandingPadInst::~LandingPadInst() { |
| 205 | dropHungoffUses(); |
| 206 | } |
| 207 | |
| 208 | LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, |
| 209 | unsigned NumReservedClauses, |
| 210 | const Twine &NameStr, |
| 211 | Instruction *InsertBefore) { |
| 212 | return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, |
| 213 | InsertBefore); |
| 214 | } |
| 215 | |
| 216 | LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, |
| 217 | unsigned NumReservedClauses, |
| 218 | const Twine &NameStr, |
| 219 | BasicBlock *InsertAtEnd) { |
| 220 | return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, |
| 221 | InsertAtEnd); |
| 222 | } |
| 223 | |
| 224 | void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues, |
| 225 | const Twine &NameStr) { |
| 226 | ReservedSpace = NumReservedValues; |
| 227 | NumOperands = 1; |
| 228 | OperandList = allocHungoffUses(ReservedSpace); |
| 229 | OperandList[0] = PersFn; |
| 230 | setName(NameStr); |
| 231 | setCleanup(false); |
| 232 | } |
| 233 | |
| 234 | /// growOperands - grow operands - This grows the operand list in response to a |
| 235 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 236 | void LandingPadInst::growOperands(unsigned Size) { |
| 237 | unsigned e = getNumOperands(); |
| 238 | if (ReservedSpace >= e + Size) return; |
| 239 | ReservedSpace = (e + Size / 2) * 2; |
| 240 | |
| 241 | Use *NewOps = allocHungoffUses(ReservedSpace); |
| 242 | Use *OldOps = OperandList; |
| 243 | for (unsigned i = 0; i != e; ++i) |
| 244 | NewOps[i] = OldOps[i]; |
| 245 | |
| 246 | OperandList = NewOps; |
| 247 | Use::zap(OldOps, OldOps + e, true); |
| 248 | } |
| 249 | |
| 250 | void LandingPadInst::addClause(Value *Val) { |
| 251 | unsigned OpNo = getNumOperands(); |
| 252 | growOperands(1); |
| 253 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 254 | ++NumOperands; |
| 255 | OperandList[OpNo] = Val; |
| 256 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 257 | |
| 258 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 259 | // CallInst Implementation |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 262 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 265 | void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) { |
| 266 | assert(NumOperands == Args.size() + 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 267 | Op<-1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 268 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 269 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 270 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 271 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 272 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 273 | assert((Args.size() == FTy->getNumParams() || |
| 274 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 275 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 276 | |
| 277 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 278 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 279 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 280 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 281 | #endif |
| 282 | |
| 283 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 284 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 287 | void CallInst::init(Value *Func, const Twine &NameStr) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 288 | assert(NumOperands == 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 289 | Op<-1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 290 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 291 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 292 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 293 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 294 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 295 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 296 | #endif |
| 297 | |
| 298 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 301 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 302 | Instruction *InsertBefore) |
| 303 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 304 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 305 | Instruction::Call, |
| 306 | OperandTraits<CallInst>::op_end(this) - 1, |
| 307 | 1, InsertBefore) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 308 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 311 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | BasicBlock *InsertAtEnd) |
| 313 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 314 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 315 | Instruction::Call, |
| 316 | OperandTraits<CallInst>::op_end(this) - 1, |
| 317 | 1, InsertAtEnd) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 318 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 321 | CallInst::CallInst(const CallInst &CI) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 322 | : Instruction(CI.getType(), Instruction::Call, |
| 323 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 324 | CI.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 325 | setAttributes(CI.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 326 | setTailCall(CI.isTailCall()); |
| 327 | setCallingConv(CI.getCallingConv()); |
| 328 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 329 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 330 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 333 | void CallInst::addAttribute(unsigned i, Attributes attr) { |
| 334 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 335 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 336 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 339 | void CallInst::removeAttribute(unsigned i, Attributes attr) { |
| 340 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 341 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 342 | setAttributes(PAL); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 345 | bool CallInst::fnHasNoAliasAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 346 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoAlias)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 347 | return true; |
| 348 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 349 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoAlias); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | bool CallInst::fnHasNoInlineAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 353 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoInline)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 354 | return true; |
| 355 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 356 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoInline); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | bool CallInst::fnHasNoReturnAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 360 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoReturn)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 361 | return true; |
| 362 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 363 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoReturn); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | bool CallInst::fnHasNoUnwindAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 367 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 368 | return true; |
| 369 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 370 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | bool CallInst::fnHasReadNoneAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 374 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadNone)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 375 | return true; |
| 376 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 377 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadNone); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | bool CallInst::fnHasReadOnlyAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 381 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 382 | return true; |
| 383 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 384 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | bool CallInst::fnHasReturnsTwiceAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 388 | if (AttributeList.getParamAttributes(~0U). |
| 389 | hasAttribute(Attributes::ReturnsTwice)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 390 | return true; |
| 391 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 392 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReturnsTwice); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 396 | bool CallInst::paramHasSExtAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 397 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::SExt)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 398 | return true; |
| 399 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 400 | return F->getParamAttributes(i).hasAttribute(Attributes::SExt); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | |
| 404 | bool CallInst::paramHasZExtAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 405 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ZExt)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 406 | return true; |
| 407 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 408 | return F->getParamAttributes(i).hasAttribute(Attributes::ZExt); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | |
| 412 | bool CallInst::paramHasInRegAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 413 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::InReg)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 414 | return true; |
| 415 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 416 | return F->getParamAttributes(i).hasAttribute(Attributes::InReg); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | |
| 420 | bool CallInst::paramHasStructRetAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 421 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::StructRet)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 422 | return true; |
| 423 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 424 | return F->getParamAttributes(i).hasAttribute(Attributes::StructRet); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | |
| 428 | bool CallInst::paramHasNestAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 429 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::Nest)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 430 | return true; |
| 431 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 432 | return F->getParamAttributes(i).hasAttribute(Attributes::Nest); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
| 436 | bool CallInst::paramHasByValAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 437 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ByVal)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 438 | return true; |
| 439 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 440 | return F->getParamAttributes(i).hasAttribute(Attributes::ByVal); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 444 | bool CallInst::paramHasNoAliasAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 445 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoAlias)) |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 446 | return true; |
| 447 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 448 | return F->getParamAttributes(i).hasAttribute(Attributes::NoAlias); |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 452 | bool CallInst::paramHasNoCaptureAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 453 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoCapture)) |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 454 | return true; |
| 455 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 456 | return F->getParamAttributes(i).hasAttribute(Attributes::NoCapture); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 460 | /// IsConstantOne - Return true only if val is constant int 1 |
| 461 | static bool IsConstantOne(Value *val) { |
| 462 | assert(val && "IsConstantOne does not work with NULL val"); |
| 463 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 464 | } |
| 465 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 466 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 467 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 468 | Type *AllocTy, Value *AllocSize, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 469 | Value *ArraySize, Function *MallocF, |
| 470 | const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 471 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 472 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 473 | |
| 474 | // malloc(type) becomes: |
| 475 | // bitcast (i8* malloc(typeSize)) to type* |
| 476 | // malloc(type, arraySize) becomes: |
| 477 | // bitcast (i8 *malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 478 | if (!ArraySize) |
| 479 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 480 | else if (ArraySize->getType() != IntPtrTy) { |
| 481 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 482 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 483 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 484 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 485 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 486 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 487 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 488 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 489 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 490 | if (IsConstantOne(AllocSize)) { |
| 491 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 492 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 493 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 494 | false /*ZExt*/); |
| 495 | // Malloc arg is constant product of type size and array size |
| 496 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 497 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 498 | // Multiply type size by the array size... |
| 499 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 500 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 501 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 502 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 503 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 504 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 505 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 506 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 507 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 508 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 509 | // Create the call to Malloc. |
| 510 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 511 | Module* M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 512 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 513 | Value *MallocFunc = MallocF; |
| 514 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 515 | // prototype malloc as "void *malloc(size_t)" |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 516 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 517 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 518 | CallInst *MCall = NULL; |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 519 | Instruction *Result = NULL; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 520 | if (InsertBefore) { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 521 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 522 | Result = MCall; |
| 523 | if (Result->getType() != AllocPtrType) |
| 524 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 525 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 526 | } else { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 527 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 528 | Result = MCall; |
| 529 | if (Result->getType() != AllocPtrType) { |
| 530 | InsertAtEnd->getInstList().push_back(MCall); |
| 531 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 532 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 533 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 534 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 535 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 536 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 537 | MCall->setCallingConv(F->getCallingConv()); |
| 538 | if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); |
| 539 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 540 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 541 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 542 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 546 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 547 | /// possibly multiplied by the array size if the array size is not |
| 548 | /// constant 1. |
| 549 | /// 2. Call malloc with that argument. |
| 550 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 551 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 552 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 553 | Value *AllocSize, Value *ArraySize, |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 554 | Function * MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 555 | const Twine &Name) { |
| 556 | return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize, |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 557 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 561 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 562 | /// possibly multiplied by the array size if the array size is not |
| 563 | /// constant 1. |
| 564 | /// 2. Call malloc with that argument. |
| 565 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 566 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 567 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 568 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 569 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 570 | Value *AllocSize, Value *ArraySize, |
| 571 | Function *MallocF, const Twine &Name) { |
| 572 | return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 573 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 574 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 575 | |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 576 | static Instruction* createFree(Value* Source, Instruction *InsertBefore, |
| 577 | BasicBlock *InsertAtEnd) { |
| 578 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 579 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 580 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 581 | "Can not free something of nonpointer type!"); |
| 582 | |
| 583 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 584 | Module* M = BB->getParent()->getParent(); |
| 585 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 586 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 587 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 588 | // prototype free as "void free(void*)" |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 589 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 590 | CallInst* Result = NULL; |
| 591 | Value *PtrCast = Source; |
| 592 | if (InsertBefore) { |
| 593 | if (Source->getType() != IntPtrTy) |
| 594 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
| 595 | Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); |
| 596 | } else { |
| 597 | if (Source->getType() != IntPtrTy) |
| 598 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
| 599 | Result = CallInst::Create(FreeFunc, PtrCast, ""); |
| 600 | } |
| 601 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 602 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 603 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 604 | |
| 605 | return Result; |
| 606 | } |
| 607 | |
| 608 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 609 | Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { |
| 610 | return createFree(Source, InsertBefore, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 614 | /// Note: This function does not add the call to the basic block, that is the |
| 615 | /// responsibility of the caller. |
| 616 | Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { |
| 617 | Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd); |
| 618 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 619 | return FreeCall; |
| 620 | } |
| 621 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 622 | //===----------------------------------------------------------------------===// |
| 623 | // InvokeInst Implementation |
| 624 | //===----------------------------------------------------------------------===// |
| 625 | |
| 626 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 627 | ArrayRef<Value *> Args, const Twine &NameStr) { |
| 628 | assert(NumOperands == 3 + Args.size() && "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 629 | Op<-3>() = Fn; |
| 630 | Op<-2>() = IfNormal; |
| 631 | Op<-1>() = IfException; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 632 | |
| 633 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 634 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 635 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 636 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 637 | assert(((Args.size() == FTy->getNumParams()) || |
| 638 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 639 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 640 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 641 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 642 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 643 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 644 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 645 | #endif |
| 646 | |
| 647 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 648 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 651 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 652 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 653 | OperandTraits<InvokeInst>::op_end(this) |
| 654 | - II.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 655 | II.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 656 | setAttributes(II.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 657 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 658 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 659 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 662 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 663 | return getSuccessor(idx); |
| 664 | } |
| 665 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 666 | return getNumSuccessors(); |
| 667 | } |
| 668 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 669 | return setSuccessor(idx, B); |
| 670 | } |
| 671 | |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 672 | bool InvokeInst::fnHasNoAliasAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 673 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoAlias)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 674 | return true; |
| 675 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 676 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoAlias); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 677 | return false; |
| 678 | } |
| 679 | bool InvokeInst::fnHasNoInlineAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 680 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoInline)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 681 | return true; |
| 682 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 683 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoInline); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | bool InvokeInst::fnHasNoReturnAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 687 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoReturn)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 688 | return true; |
| 689 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 690 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoReturn); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 691 | return false; |
| 692 | } |
| 693 | bool InvokeInst::fnHasNoUnwindAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 694 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 695 | return true; |
| 696 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 697 | return F->getParamAttributes(~0U).hasAttribute(Attributes::NoUnwind); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 698 | return false; |
| 699 | } |
| 700 | bool InvokeInst::fnHasReadNoneAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 701 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadNone)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 702 | return true; |
| 703 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 704 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadNone); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 705 | return false; |
| 706 | } |
| 707 | bool InvokeInst::fnHasReadOnlyAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 708 | if (AttributeList.getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 709 | return true; |
| 710 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 711 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReadOnly); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | bool InvokeInst::fnHasReturnsTwiceAttr() const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 715 | if (AttributeList.getParamAttributes(~0U). |
| 716 | hasAttribute(Attributes::ReturnsTwice)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 717 | return true; |
| 718 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 719 | return F->getParamAttributes(~0U).hasAttribute(Attributes::ReturnsTwice); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 720 | return false; |
| 721 | } |
| 722 | |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 723 | bool InvokeInst::paramHasSExtAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 724 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::SExt)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 725 | return true; |
| 726 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 727 | return F->getParamAttributes(i).hasAttribute(Attributes::SExt); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 728 | return false; |
| 729 | } |
| 730 | |
| 731 | bool InvokeInst::paramHasZExtAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 732 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ZExt)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 733 | return true; |
| 734 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 735 | return F->getParamAttributes(i).hasAttribute(Attributes::ZExt); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
| 739 | bool InvokeInst::paramHasInRegAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 740 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::InReg)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 741 | return true; |
| 742 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 743 | return F->getParamAttributes(i).hasAttribute(Attributes::InReg); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
| 747 | bool InvokeInst::paramHasStructRetAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 748 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::StructRet)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 749 | return true; |
| 750 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 751 | return F->getParamAttributes(i).hasAttribute(Attributes::StructRet); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
| 755 | bool InvokeInst::paramHasNestAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 756 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::Nest)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 757 | return true; |
| 758 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 759 | return F->getParamAttributes(i).hasAttribute(Attributes::Nest); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 760 | return false; |
| 761 | } |
| 762 | |
| 763 | bool InvokeInst::paramHasByValAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 764 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::ByVal)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 765 | return true; |
| 766 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 767 | return F->getParamAttributes(i).hasAttribute(Attributes::ByVal); |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 771 | bool InvokeInst::paramHasNoAliasAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 772 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoAlias)) |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 773 | return true; |
| 774 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 775 | return F->getParamAttributes(i).hasAttribute(Attributes::NoAlias); |
Bill Wendling | d777398 | 2012-10-04 06:52:09 +0000 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 779 | bool InvokeInst::paramHasNoCaptureAttr(unsigned i) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 780 | if (AttributeList.getParamAttributes(i).hasAttribute(Attributes::NoCapture)) |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 781 | return true; |
| 782 | if (const Function *F = getCalledFunction()) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 783 | return F->getParamAttributes(i).hasAttribute(Attributes::NoCapture); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 784 | return false; |
| 785 | } |
| 786 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 787 | void InvokeInst::addAttribute(unsigned i, Attributes attr) { |
| 788 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 789 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 790 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 793 | void InvokeInst::removeAttribute(unsigned i, Attributes attr) { |
| 794 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 795 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 796 | setAttributes(PAL); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 799 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 800 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 801 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 802 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 803 | //===----------------------------------------------------------------------===// |
| 804 | // ReturnInst Implementation |
| 805 | //===----------------------------------------------------------------------===// |
| 806 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 807 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 808 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 809 | OperandTraits<ReturnInst>::op_end(this) - |
| 810 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 811 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 812 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 813 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 814 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 817 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 818 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 819 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 820 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 821 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 822 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 823 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 824 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 825 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 826 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 827 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 828 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 829 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 830 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 831 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 832 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 833 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 836 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 837 | return getNumSuccessors(); |
| 838 | } |
| 839 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 840 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 841 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 842 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 843 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 846 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 847 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 850 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 851 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 852 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 853 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 854 | // ResumeInst Implementation |
| 855 | //===----------------------------------------------------------------------===// |
| 856 | |
| 857 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 858 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 859 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 860 | Op<0>() = RI.Op<0>(); |
| 861 | } |
| 862 | |
| 863 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 864 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 865 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 866 | Op<0>() = Exn; |
| 867 | } |
| 868 | |
| 869 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 870 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 871 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 872 | Op<0>() = Exn; |
| 873 | } |
| 874 | |
| 875 | unsigned ResumeInst::getNumSuccessorsV() const { |
| 876 | return getNumSuccessors(); |
| 877 | } |
| 878 | |
| 879 | void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 880 | llvm_unreachable("ResumeInst has no successors!"); |
| 881 | } |
| 882 | |
| 883 | BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { |
| 884 | llvm_unreachable("ResumeInst has no successors!"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 888 | // UnreachableInst Implementation |
| 889 | //===----------------------------------------------------------------------===// |
| 890 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 891 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 892 | Instruction *InsertBefore) |
| 893 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 894 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 895 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 896 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 897 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 898 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 901 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 902 | return getNumSuccessors(); |
| 903 | } |
| 904 | |
| 905 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 906 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 910 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 914 | // BranchInst Implementation |
| 915 | //===----------------------------------------------------------------------===// |
| 916 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 917 | void BranchInst::AssertOK() { |
| 918 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 919 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 920 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 923 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 924 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 925 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 926 | 1, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 927 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 928 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 929 | } |
| 930 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 931 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 932 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 933 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 934 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 935 | Op<-1>() = IfTrue; |
| 936 | Op<-2>() = IfFalse; |
| 937 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 938 | #ifndef NDEBUG |
| 939 | AssertOK(); |
| 940 | #endif |
| 941 | } |
| 942 | |
| 943 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 944 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 945 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 946 | 1, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 947 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 948 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 952 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 953 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 954 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 955 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 956 | Op<-1>() = IfTrue; |
| 957 | Op<-2>() = IfFalse; |
| 958 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 959 | #ifndef NDEBUG |
| 960 | AssertOK(); |
| 961 | #endif |
| 962 | } |
| 963 | |
| 964 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 965 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 966 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 967 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 968 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 969 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 970 | if (BI.getNumOperands() != 1) { |
| 971 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 972 | Op<-3>() = BI.Op<-3>(); |
| 973 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 974 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 975 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 978 | void BranchInst::swapSuccessors() { |
| 979 | assert(isConditional() && |
| 980 | "Cannot swap successors of an unconditional branch"); |
| 981 | Op<-1>().swap(Op<-2>()); |
| 982 | |
| 983 | // Update profile metadata if present and it matches our structural |
| 984 | // expectations. |
| 985 | MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 986 | if (!ProfileData || ProfileData->getNumOperands() != 3) |
| 987 | return; |
| 988 | |
| 989 | // The first operand is the name. Fetch them backwards and build a new one. |
| 990 | Value *Ops[] = { |
| 991 | ProfileData->getOperand(0), |
| 992 | ProfileData->getOperand(2), |
| 993 | ProfileData->getOperand(1) |
| 994 | }; |
| 995 | setMetadata(LLVMContext::MD_prof, |
| 996 | MDNode::get(ProfileData->getContext(), Ops)); |
| 997 | } |
| 998 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 999 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 1000 | return getSuccessor(idx); |
| 1001 | } |
| 1002 | unsigned BranchInst::getNumSuccessorsV() const { |
| 1003 | return getNumSuccessors(); |
| 1004 | } |
| 1005 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1006 | setSuccessor(idx, B); |
| 1007 | } |
| 1008 | |
| 1009 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1010 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1011 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1012 | //===----------------------------------------------------------------------===// |
| 1013 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1014 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1015 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1016 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1017 | else { |
| 1018 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1019 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1020 | assert(Amt->getType()->isIntegerTy() && |
| 1021 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1022 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1023 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1026 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1027 | const Twine &Name, Instruction *InsertBefore) |
| 1028 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1029 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
| 1030 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1031 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1032 | setName(Name); |
| 1033 | } |
| 1034 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1035 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1036 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1037 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1038 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
| 1039 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1040 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1041 | setName(Name); |
| 1042 | } |
| 1043 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1044 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1045 | Instruction *InsertBefore) |
| 1046 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1047 | getAISize(Ty->getContext(), 0), InsertBefore) { |
| 1048 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1049 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1050 | setName(Name); |
| 1051 | } |
| 1052 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1053 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1054 | BasicBlock *InsertAtEnd) |
| 1055 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1056 | getAISize(Ty->getContext(), 0), InsertAtEnd) { |
| 1057 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1058 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1059 | setName(Name); |
| 1060 | } |
| 1061 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1062 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1063 | const Twine &Name, Instruction *InsertBefore) |
| 1064 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 1065 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1066 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1067 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1068 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1071 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1072 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1073 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 1074 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1075 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1076 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1077 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1080 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1081 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1084 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1085 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1086 | assert(Align <= MaximumAlignment && |
| 1087 | "Alignment is greater than MaximumAlignment!"); |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1088 | setInstructionSubclassData(Log2_32(Align) + 1); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1089 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1090 | } |
| 1091 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1092 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1093 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1094 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1095 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1098 | Type *AllocaInst::getAllocatedType() const { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1099 | return getType()->getElementType(); |
| 1100 | } |
| 1101 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1102 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1103 | /// function and is a constant size. If so, the code generator will fold it |
| 1104 | /// into the prolog/epilog code, so it is basically free. |
| 1105 | bool AllocaInst::isStaticAlloca() const { |
| 1106 | // Must be constant size. |
| 1107 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 1108 | |
| 1109 | // Must be in the entry block. |
| 1110 | const BasicBlock *Parent = getParent(); |
| 1111 | return Parent == &Parent->getParent()->front(); |
| 1112 | } |
| 1113 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1114 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1115 | // LoadInst Implementation |
| 1116 | //===----------------------------------------------------------------------===// |
| 1117 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1118 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1119 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1120 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1121 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1122 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1125 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1126 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1127 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1128 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1129 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1130 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1131 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1132 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1135 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1136 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1137 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1138 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1139 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1140 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1141 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1142 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1145 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1146 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1147 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1148 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1149 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1150 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1151 | setAtomic(NotAtomic); |
| 1152 | AssertOK(); |
| 1153 | setName(Name); |
| 1154 | } |
| 1155 | |
| 1156 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1157 | BasicBlock *InsertAE) |
| 1158 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1159 | Load, Ptr, InsertAE) { |
| 1160 | setVolatile(isVolatile); |
| 1161 | setAlignment(0); |
| 1162 | setAtomic(NotAtomic); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1163 | AssertOK(); |
| 1164 | setName(Name); |
| 1165 | } |
| 1166 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1167 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1168 | unsigned Align, Instruction *InsertBef) |
| 1169 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1170 | Load, Ptr, InsertBef) { |
| 1171 | setVolatile(isVolatile); |
| 1172 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1173 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1174 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1175 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1178 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1179 | unsigned Align, BasicBlock *InsertAE) |
| 1180 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1181 | Load, Ptr, InsertAE) { |
| 1182 | setVolatile(isVolatile); |
| 1183 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1184 | setAtomic(NotAtomic); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1185 | AssertOK(); |
| 1186 | setName(Name); |
| 1187 | } |
| 1188 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1189 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1190 | unsigned Align, AtomicOrdering Order, |
| 1191 | SynchronizationScope SynchScope, |
| 1192 | Instruction *InsertBef) |
| 1193 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1194 | Load, Ptr, InsertBef) { |
| 1195 | setVolatile(isVolatile); |
| 1196 | setAlignment(Align); |
| 1197 | setAtomic(Order, SynchScope); |
| 1198 | AssertOK(); |
| 1199 | setName(Name); |
| 1200 | } |
| 1201 | |
| 1202 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1203 | unsigned Align, AtomicOrdering Order, |
| 1204 | SynchronizationScope SynchScope, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1205 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1206 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1207 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1208 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1209 | setAlignment(Align); |
| 1210 | setAtomic(Order, SynchScope); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1211 | AssertOK(); |
| 1212 | setName(Name); |
| 1213 | } |
| 1214 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1215 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 1216 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1217 | Load, Ptr, InsertBef) { |
| 1218 | setVolatile(false); |
| 1219 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1220 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1221 | AssertOK(); |
| 1222 | if (Name && Name[0]) setName(Name); |
| 1223 | } |
| 1224 | |
| 1225 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1226 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1227 | Load, Ptr, InsertAE) { |
| 1228 | setVolatile(false); |
| 1229 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1230 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1231 | AssertOK(); |
| 1232 | if (Name && Name[0]) setName(Name); |
| 1233 | } |
| 1234 | |
| 1235 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1236 | Instruction *InsertBef) |
| 1237 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1238 | Load, Ptr, InsertBef) { |
| 1239 | setVolatile(isVolatile); |
| 1240 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1241 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1242 | AssertOK(); |
| 1243 | if (Name && Name[0]) setName(Name); |
| 1244 | } |
| 1245 | |
| 1246 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1247 | BasicBlock *InsertAE) |
| 1248 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1249 | Load, Ptr, InsertAE) { |
| 1250 | setVolatile(isVolatile); |
| 1251 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1252 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1253 | AssertOK(); |
| 1254 | if (Name && Name[0]) setName(Name); |
| 1255 | } |
| 1256 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1257 | void LoadInst::setAlignment(unsigned Align) { |
| 1258 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1259 | assert(Align <= MaximumAlignment && |
| 1260 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1261 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1262 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1263 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1264 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1265 | |
| 1266 | //===----------------------------------------------------------------------===// |
| 1267 | // StoreInst Implementation |
| 1268 | //===----------------------------------------------------------------------===// |
| 1269 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1270 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1271 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1272 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1273 | "Ptr must have pointer type!"); |
| 1274 | assert(getOperand(0)->getType() == |
| 1275 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1276 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1277 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1278 | "Alignment required for atomic load"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1279 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1281 | |
| 1282 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1283 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1284 | OperandTraits<StoreInst>::op_begin(this), |
| 1285 | OperandTraits<StoreInst>::operands(this), |
| 1286 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1287 | Op<0>() = val; |
| 1288 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1289 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1290 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1291 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1292 | AssertOK(); |
| 1293 | } |
| 1294 | |
| 1295 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1296 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1297 | OperandTraits<StoreInst>::op_begin(this), |
| 1298 | OperandTraits<StoreInst>::operands(this), |
| 1299 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1300 | Op<0>() = val; |
| 1301 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1302 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1303 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1304 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1305 | AssertOK(); |
| 1306 | } |
| 1307 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1308 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1309 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1310 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1311 | OperandTraits<StoreInst>::op_begin(this), |
| 1312 | OperandTraits<StoreInst>::operands(this), |
| 1313 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1314 | Op<0>() = val; |
| 1315 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1316 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1317 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1318 | setAtomic(NotAtomic); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1319 | AssertOK(); |
| 1320 | } |
| 1321 | |
| 1322 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1323 | unsigned Align, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1324 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1325 | OperandTraits<StoreInst>::op_begin(this), |
| 1326 | OperandTraits<StoreInst>::operands(this), |
| 1327 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1328 | Op<0>() = val; |
| 1329 | Op<1>() = addr; |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1330 | setVolatile(isVolatile); |
| 1331 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1332 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1333 | AssertOK(); |
| 1334 | } |
| 1335 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1336 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1337 | unsigned Align, AtomicOrdering Order, |
| 1338 | SynchronizationScope SynchScope, |
| 1339 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1340 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1341 | OperandTraits<StoreInst>::op_begin(this), |
| 1342 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1343 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1344 | Op<0>() = val; |
| 1345 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1346 | setVolatile(isVolatile); |
| 1347 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1348 | setAtomic(Order, SynchScope); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1349 | AssertOK(); |
| 1350 | } |
| 1351 | |
| 1352 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1353 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1354 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1355 | OperandTraits<StoreInst>::op_begin(this), |
| 1356 | OperandTraits<StoreInst>::operands(this), |
| 1357 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1358 | Op<0>() = val; |
| 1359 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1360 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1361 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1362 | setAtomic(NotAtomic); |
| 1363 | AssertOK(); |
| 1364 | } |
| 1365 | |
| 1366 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1367 | unsigned Align, BasicBlock *InsertAtEnd) |
| 1368 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1369 | OperandTraits<StoreInst>::op_begin(this), |
| 1370 | OperandTraits<StoreInst>::operands(this), |
| 1371 | InsertAtEnd) { |
| 1372 | Op<0>() = val; |
| 1373 | Op<1>() = addr; |
| 1374 | setVolatile(isVolatile); |
| 1375 | setAlignment(Align); |
| 1376 | setAtomic(NotAtomic); |
| 1377 | AssertOK(); |
| 1378 | } |
| 1379 | |
| 1380 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1381 | unsigned Align, AtomicOrdering Order, |
| 1382 | SynchronizationScope SynchScope, |
| 1383 | BasicBlock *InsertAtEnd) |
| 1384 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1385 | OperandTraits<StoreInst>::op_begin(this), |
| 1386 | OperandTraits<StoreInst>::operands(this), |
| 1387 | InsertAtEnd) { |
| 1388 | Op<0>() = val; |
| 1389 | Op<1>() = addr; |
| 1390 | setVolatile(isVolatile); |
| 1391 | setAlignment(Align); |
| 1392 | setAtomic(Order, SynchScope); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1393 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1396 | void StoreInst::setAlignment(unsigned Align) { |
| 1397 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1398 | assert(Align <= MaximumAlignment && |
| 1399 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1400 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1401 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1402 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1405 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1406 | // AtomicCmpXchgInst Implementation |
| 1407 | //===----------------------------------------------------------------------===// |
| 1408 | |
| 1409 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1410 | AtomicOrdering Ordering, |
| 1411 | SynchronizationScope SynchScope) { |
| 1412 | Op<0>() = Ptr; |
| 1413 | Op<1>() = Cmp; |
| 1414 | Op<2>() = NewVal; |
| 1415 | setOrdering(Ordering); |
| 1416 | setSynchScope(SynchScope); |
| 1417 | |
| 1418 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1419 | "All operands must be non-null!"); |
| 1420 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1421 | "Ptr must have pointer type!"); |
| 1422 | assert(getOperand(1)->getType() == |
| 1423 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1424 | && "Ptr must be a pointer to Cmp type!"); |
| 1425 | assert(getOperand(2)->getType() == |
| 1426 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1427 | && "Ptr must be a pointer to NewVal type!"); |
| 1428 | assert(Ordering != NotAtomic && |
| 1429 | "AtomicCmpXchg instructions must be atomic!"); |
| 1430 | } |
| 1431 | |
| 1432 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1433 | AtomicOrdering Ordering, |
| 1434 | SynchronizationScope SynchScope, |
| 1435 | Instruction *InsertBefore) |
| 1436 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1437 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1438 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1439 | InsertBefore) { |
| 1440 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1441 | } |
| 1442 | |
| 1443 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1444 | AtomicOrdering Ordering, |
| 1445 | SynchronizationScope SynchScope, |
| 1446 | BasicBlock *InsertAtEnd) |
| 1447 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1448 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1449 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1450 | InsertAtEnd) { |
| 1451 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1452 | } |
| 1453 | |
| 1454 | //===----------------------------------------------------------------------===// |
| 1455 | // AtomicRMWInst Implementation |
| 1456 | //===----------------------------------------------------------------------===// |
| 1457 | |
| 1458 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1459 | AtomicOrdering Ordering, |
| 1460 | SynchronizationScope SynchScope) { |
| 1461 | Op<0>() = Ptr; |
| 1462 | Op<1>() = Val; |
| 1463 | setOperation(Operation); |
| 1464 | setOrdering(Ordering); |
| 1465 | setSynchScope(SynchScope); |
| 1466 | |
| 1467 | assert(getOperand(0) && getOperand(1) && |
| 1468 | "All operands must be non-null!"); |
| 1469 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1470 | "Ptr must have pointer type!"); |
| 1471 | assert(getOperand(1)->getType() == |
| 1472 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1473 | && "Ptr must be a pointer to Val type!"); |
| 1474 | assert(Ordering != NotAtomic && |
| 1475 | "AtomicRMW instructions must be atomic!"); |
| 1476 | } |
| 1477 | |
| 1478 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1479 | AtomicOrdering Ordering, |
| 1480 | SynchronizationScope SynchScope, |
| 1481 | Instruction *InsertBefore) |
| 1482 | : Instruction(Val->getType(), AtomicRMW, |
| 1483 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1484 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1485 | InsertBefore) { |
| 1486 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1487 | } |
| 1488 | |
| 1489 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1490 | AtomicOrdering Ordering, |
| 1491 | SynchronizationScope SynchScope, |
| 1492 | BasicBlock *InsertAtEnd) |
| 1493 | : Instruction(Val->getType(), AtomicRMW, |
| 1494 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1495 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1496 | InsertAtEnd) { |
| 1497 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1498 | } |
| 1499 | |
| 1500 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1501 | // FenceInst Implementation |
| 1502 | //===----------------------------------------------------------------------===// |
| 1503 | |
| 1504 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1505 | SynchronizationScope SynchScope, |
| 1506 | Instruction *InsertBefore) |
| 1507 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) { |
| 1508 | setOrdering(Ordering); |
| 1509 | setSynchScope(SynchScope); |
| 1510 | } |
| 1511 | |
| 1512 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1513 | SynchronizationScope SynchScope, |
| 1514 | BasicBlock *InsertAtEnd) |
| 1515 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) { |
| 1516 | setOrdering(Ordering); |
| 1517 | setSynchScope(SynchScope); |
| 1518 | } |
| 1519 | |
| 1520 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1521 | // GetElementPtrInst Implementation |
| 1522 | //===----------------------------------------------------------------------===// |
| 1523 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1524 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1525 | const Twine &Name) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1526 | assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?"); |
| 1527 | OperandList[0] = Ptr; |
| 1528 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1529 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1532 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1533 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1534 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1535 | - GEPI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1536 | GEPI.getNumOperands()) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1537 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1538 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1541 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1542 | /// a gep instruction with the specified parameters. |
| 1543 | /// |
| 1544 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1545 | /// indices, either as Value* or uint64_t. |
| 1546 | /// |
| 1547 | /// A null type is returned if the indices are invalid for the specified |
| 1548 | /// pointer type. |
| 1549 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1550 | template <typename IndexTy> |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1551 | static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1552 | if (Ptr->isVectorTy()) { |
| 1553 | assert(IdxList.size() == 1 && |
| 1554 | "GEP with vector pointers must have a single index"); |
| 1555 | PointerType *PTy = dyn_cast<PointerType>( |
| 1556 | cast<VectorType>(Ptr)->getElementType()); |
| 1557 | assert(PTy && "Gep with invalid vector pointer found"); |
| 1558 | return PTy->getElementType(); |
| 1559 | } |
| 1560 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1561 | PointerType *PTy = dyn_cast<PointerType>(Ptr); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1562 | if (!PTy) return 0; // Type isn't a pointer type! |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1563 | Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1564 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1565 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1566 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1567 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1569 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1570 | // it cannot be 'stepped over'. |
| 1571 | if (!Agg->isSized()) |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1572 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1573 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1574 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1575 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1576 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1577 | if (!CT || CT->isPointerTy()) return 0; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1578 | IndexTy Index = IdxList[CurIdx]; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1579 | if (!CT->indexValid(Index)) return 0; |
| 1580 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1581 | } |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1582 | return CurIdx == IdxList.size() ? Agg : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1585 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) { |
| 1586 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1589 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1590 | ArrayRef<Constant *> IdxList) { |
| 1591 | return getIndexedTypeInternal(Ptr, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1594 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) { |
| 1595 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1598 | unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) { |
| 1599 | Type *Ty = Ptr->getType(); |
| 1600 | |
| 1601 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 1602 | Ty = VTy->getElementType(); |
| 1603 | |
| 1604 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 1605 | return PTy->getAddressSpace(); |
| 1606 | |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 1607 | llvm_unreachable("Invalid GEP pointer type"); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1610 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1611 | /// zeros. If so, the result pointer and the first operand have the same |
| 1612 | /// value, just potentially different types. |
| 1613 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1614 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1615 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1616 | if (!CI->isZero()) return false; |
| 1617 | } else { |
| 1618 | return false; |
| 1619 | } |
| 1620 | } |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1624 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1625 | /// constant integers. If so, the result pointer and the first operand have |
| 1626 | /// a constant offset between them. |
| 1627 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1628 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1629 | if (!isa<ConstantInt>(getOperand(i))) |
| 1630 | return false; |
| 1631 | } |
| 1632 | return true; |
| 1633 | } |
| 1634 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1635 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1636 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1637 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1638 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1639 | bool GetElementPtrInst::isInBounds() const { |
| 1640 | return cast<GEPOperator>(this)->isInBounds(); |
| 1641 | } |
| 1642 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1643 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1644 | // ExtractElementInst Implementation |
| 1645 | //===----------------------------------------------------------------------===// |
| 1646 | |
| 1647 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1648 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1649 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1650 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1651 | ExtractElement, |
| 1652 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1653 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1654 | assert(isValidOperands(Val, Index) && |
| 1655 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1656 | Op<0>() = Val; |
| 1657 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1658 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1662 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1663 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1664 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1665 | ExtractElement, |
| 1666 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1667 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1668 | assert(isValidOperands(Val, Index) && |
| 1669 | "Invalid extractelement instruction operands!"); |
| 1670 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1671 | Op<0>() = Val; |
| 1672 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1673 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1676 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1677 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1678 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32)) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1679 | return false; |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
| 1683 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1684 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1685 | // InsertElementInst Implementation |
| 1686 | //===----------------------------------------------------------------------===// |
| 1687 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1688 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1689 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1690 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1691 | : Instruction(Vec->getType(), InsertElement, |
| 1692 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1693 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1694 | assert(isValidOperands(Vec, Elt, Index) && |
| 1695 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1696 | Op<0>() = Vec; |
| 1697 | Op<1>() = Elt; |
| 1698 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1699 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1702 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1703 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1704 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1705 | : Instruction(Vec->getType(), InsertElement, |
| 1706 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1707 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1708 | assert(isValidOperands(Vec, Elt, Index) && |
| 1709 | "Invalid insertelement instruction operands!"); |
| 1710 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1711 | Op<0>() = Vec; |
| 1712 | Op<1>() = Elt; |
| 1713 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1714 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1717 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1718 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1719 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1720 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1721 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1722 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1723 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1724 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1725 | if (!Index->getType()->isIntegerTy(32)) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1726 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1727 | return true; |
| 1728 | } |
| 1729 | |
| 1730 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1731 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1732 | // ShuffleVectorInst Implementation |
| 1733 | //===----------------------------------------------------------------------===// |
| 1734 | |
| 1735 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1736 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1737 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1738 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1739 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1740 | ShuffleVector, |
| 1741 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1742 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1743 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1744 | assert(isValidOperands(V1, V2, Mask) && |
| 1745 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1746 | Op<0>() = V1; |
| 1747 | Op<1>() = V2; |
| 1748 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1749 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1753 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1754 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1755 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1756 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1757 | ShuffleVector, |
| 1758 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1759 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1760 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1761 | assert(isValidOperands(V1, V2, Mask) && |
| 1762 | "Invalid shuffle vector instruction operands!"); |
| 1763 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1764 | Op<0>() = V1; |
| 1765 | Op<1>() = V2; |
| 1766 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1767 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1770 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1771 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1772 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1773 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1774 | return false; |
| 1775 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1776 | // Mask must be vector of i32. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1777 | VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1778 | if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1779 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1780 | |
| 1781 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1782 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1783 | return true; |
| 1784 | |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1785 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1786 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1787 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1788 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { |
| 1789 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1790 | return false; |
| 1791 | } else if (!isa<UndefValue>(MV->getOperand(i))) { |
| 1792 | return false; |
| 1793 | } |
| 1794 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1795 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1796 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (const ConstantDataSequential *CDS = |
| 1799 | dyn_cast<ConstantDataSequential>(Mask)) { |
| 1800 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1801 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1802 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1803 | return false; |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
| 1807 | // The bitcode reader can create a place holder for a forward reference |
| 1808 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1809 | // fall into this case and fail. To avoid this error, do this bit of |
| 1810 | // ugliness to allow such a mask pass. |
| 1811 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask)) |
| 1812 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1813 | return true; |
| 1814 | |
| 1815 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1818 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1819 | /// output result. This is either -1 if the element is undef or a number less |
| 1820 | /// than 2*numelements. |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1821 | int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { |
| 1822 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
| 1823 | if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1824 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1825 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1826 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1827 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1828 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1831 | /// getShuffleMask - Return the full mask for this instruction, where each |
| 1832 | /// element is the element number and undef's are returned as -1. |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1833 | void ShuffleVectorInst::getShuffleMask(Constant *Mask, |
| 1834 | SmallVectorImpl<int> &Result) { |
| 1835 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1837 | if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1838 | for (unsigned i = 0; i != NumElts; ++i) |
| 1839 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1840 | return; |
| 1841 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1842 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1843 | Constant *C = Mask->getAggregateElement(i); |
| 1844 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1845 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1850 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1851 | // InsertValueInst Class |
| 1852 | //===----------------------------------------------------------------------===// |
| 1853 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1854 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1855 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1856 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1857 | |
| 1858 | // There's no fundamental reason why we require at least one index |
| 1859 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1860 | // getelementptr's init routine for example). But there's no |
| 1861 | // present need to support it. |
| 1862 | assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); |
| 1863 | |
| 1864 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1865 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1866 | Op<0>() = Agg; |
| 1867 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1868 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1869 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1870 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1874 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1875 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1876 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1877 | Op<0>() = IVI.getOperand(0); |
| 1878 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1879 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1883 | // ExtractValueInst Class |
| 1884 | //===----------------------------------------------------------------------===// |
| 1885 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1886 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1887 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1888 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1889 | // There's no fundamental reason why we require at least one index. |
| 1890 | // But there's no present need to support it. |
| 1891 | assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1892 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1893 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1894 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1898 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1899 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1900 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1903 | // getIndexedType - Returns the type of the element that would be extracted |
| 1904 | // with an extractvalue instruction with the specified parameters. |
| 1905 | // |
| 1906 | // A null type is returned if the indices are invalid for the specified |
| 1907 | // pointer type. |
| 1908 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1909 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1910 | ArrayRef<unsigned> Idxs) { |
| 1911 | for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1912 | unsigned Index = Idxs[CurIdx]; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1913 | // We can't use CompositeType::indexValid(Index) here. |
| 1914 | // indexValid() always returns true for arrays because getelementptr allows |
| 1915 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1916 | // insertvalue we need to check array indexing manually. |
| 1917 | // Since the only other types we can index into are struct types it's just |
| 1918 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1919 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1920 | if (Index >= AT->getNumElements()) |
| 1921 | return 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1922 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1923 | if (Index >= ST->getNumElements()) |
| 1924 | return 0; |
| 1925 | } else { |
| 1926 | // Not a valid type to index into. |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1931 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1932 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1933 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1934 | |
| 1935 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1936 | // BinaryOperator Class |
| 1937 | //===----------------------------------------------------------------------===// |
| 1938 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1939 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1940 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1941 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1942 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1943 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1944 | OperandTraits<BinaryOperator>::operands(this), |
| 1945 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1946 | Op<0>() = S1; |
| 1947 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1948 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1949 | setName(Name); |
| 1950 | } |
| 1951 | |
| 1952 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1953 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1954 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1955 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1956 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1957 | OperandTraits<BinaryOperator>::operands(this), |
| 1958 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1959 | Op<0>() = S1; |
| 1960 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1961 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1962 | setName(Name); |
| 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1967 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 1968 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1969 | assert(LHS->getType() == RHS->getType() && |
| 1970 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1971 | #ifndef NDEBUG |
| 1972 | switch (iType) { |
| 1973 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1974 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1975 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1976 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1977 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1978 | "Tried to create an integer operation on a non-integer type!"); |
| 1979 | break; |
| 1980 | case FAdd: case FSub: |
| 1981 | case FMul: |
| 1982 | assert(getType() == LHS->getType() && |
| 1983 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1984 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1985 | "Tried to create a floating-point operation on a " |
| 1986 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1987 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1988 | case UDiv: |
| 1989 | case SDiv: |
| 1990 | assert(getType() == LHS->getType() && |
| 1991 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1992 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1993 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1994 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1995 | break; |
| 1996 | case FDiv: |
| 1997 | assert(getType() == LHS->getType() && |
| 1998 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1999 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2000 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2001 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2002 | case URem: |
| 2003 | case SRem: |
| 2004 | assert(getType() == LHS->getType() && |
| 2005 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2006 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2007 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2008 | "Incorrect operand type (not integer) for S/UREM"); |
| 2009 | break; |
| 2010 | case FRem: |
| 2011 | assert(getType() == LHS->getType() && |
| 2012 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2013 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2014 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2015 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2016 | case Shl: |
| 2017 | case LShr: |
| 2018 | case AShr: |
| 2019 | assert(getType() == LHS->getType() && |
| 2020 | "Shift operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2021 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2022 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2023 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2024 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2025 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2026 | case And: case Or: |
| 2027 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2028 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2029 | "Logical operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2030 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2031 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2032 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2033 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2034 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2035 | default: |
| 2036 | break; |
| 2037 | } |
| 2038 | #endif |
| 2039 | } |
| 2040 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2041 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2042 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2043 | Instruction *InsertBefore) { |
| 2044 | assert(S1->getType() == S2->getType() && |
| 2045 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2046 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2049 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2050 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2051 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2052 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2053 | InsertAtEnd->getInstList().push_back(Res); |
| 2054 | return Res; |
| 2055 | } |
| 2056 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2057 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2058 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2059 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2060 | return new BinaryOperator(Instruction::Sub, |
| 2061 | zero, Op, |
| 2062 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2065 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2066 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2067 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2068 | return new BinaryOperator(Instruction::Sub, |
| 2069 | zero, Op, |
| 2070 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2073 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2074 | Instruction *InsertBefore) { |
| 2075 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2076 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2077 | } |
| 2078 | |
| 2079 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2080 | BasicBlock *InsertAtEnd) { |
| 2081 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2082 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2083 | } |
| 2084 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2085 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2086 | Instruction *InsertBefore) { |
| 2087 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2088 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2089 | } |
| 2090 | |
| 2091 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2092 | BasicBlock *InsertAtEnd) { |
| 2093 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2094 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2095 | } |
| 2096 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2097 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2098 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2099 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2100 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2101 | Op->getType(), Name, InsertBefore); |
| 2102 | } |
| 2103 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2104 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2105 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2106 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2107 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2108 | Op->getType(), Name, InsertAtEnd); |
| 2109 | } |
| 2110 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2111 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2112 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2113 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2114 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2115 | Op->getType(), Name, InsertBefore); |
| 2116 | } |
| 2117 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2118 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2119 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2120 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2121 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2122 | Op->getType(), Name, InsertAtEnd); |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | // isConstantAllOnes - Helper function for several functions below |
| 2127 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2128 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 2129 | return C->isAllOnesValue(); |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 2130 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2133 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2134 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2135 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2136 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 2137 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2138 | return false; |
| 2139 | } |
| 2140 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2141 | bool BinaryOperator::isFNeg(const Value *V) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2142 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2143 | if (Bop->getOpcode() == Instruction::FSub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2144 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
Dan Gohman | 11ff570 | 2009-09-11 00:05:10 +0000 | [diff] [blame] | 2145 | return C->isNegativeZeroValue(); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2146 | return false; |
| 2147 | } |
| 2148 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2149 | bool BinaryOperator::isNot(const Value *V) { |
| 2150 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2151 | return (Bop->getOpcode() == Instruction::Xor && |
| 2152 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 2153 | isConstantAllOnes(Bop->getOperand(0)))); |
| 2154 | return false; |
| 2155 | } |
| 2156 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2157 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2158 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2161 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 2162 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2165 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2166 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 2167 | } |
| 2168 | |
| 2169 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 2170 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 2171 | } |
| 2172 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2173 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 2174 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 2175 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 2176 | Value *Op0 = BO->getOperand(0); |
| 2177 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2178 | if (isConstantAllOnes(Op0)) return Op1; |
| 2179 | |
| 2180 | assert(isConstantAllOnes(Op1)); |
| 2181 | return Op0; |
| 2182 | } |
| 2183 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2184 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 2185 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | // swapOperands - Exchange the two operands to this instruction. This |
| 2190 | // instruction is safe to use on any binary instruction and does not |
| 2191 | // modify the semantics of the instruction. If the instruction is |
| 2192 | // order dependent (SetLT f.e.) the opcode is changed. |
| 2193 | // |
| 2194 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2195 | if (!isCommutative()) |
| 2196 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2197 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2198 | return false; |
| 2199 | } |
| 2200 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2201 | void BinaryOperator::setHasNoUnsignedWrap(bool b) { |
| 2202 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 2203 | } |
| 2204 | |
| 2205 | void BinaryOperator::setHasNoSignedWrap(bool b) { |
| 2206 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 2207 | } |
| 2208 | |
| 2209 | void BinaryOperator::setIsExact(bool b) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2210 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2213 | bool BinaryOperator::hasNoUnsignedWrap() const { |
| 2214 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 2215 | } |
| 2216 | |
| 2217 | bool BinaryOperator::hasNoSignedWrap() const { |
| 2218 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 2219 | } |
| 2220 | |
| 2221 | bool BinaryOperator::isExact() const { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2222 | return cast<PossiblyExactOperator>(this)->isExact(); |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2225 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2226 | // FPMathOperator Class |
| 2227 | //===----------------------------------------------------------------------===// |
| 2228 | |
| 2229 | /// getFPAccuracy - Get the maximum error permitted by this operation in ULPs. |
| 2230 | /// An accuracy of 0.0 means that the operation should be performed with the |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2231 | /// default precision. |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2232 | float FPMathOperator::getFPAccuracy() const { |
| 2233 | const MDNode *MD = |
| 2234 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
| 2235 | if (!MD) |
| 2236 | return 0.0; |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2237 | ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0)); |
| 2238 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | |
| 2242 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2243 | // CastInst Class |
| 2244 | //===----------------------------------------------------------------------===// |
| 2245 | |
David Blaikie | 3a15e14 | 2011-12-01 08:00:17 +0000 | [diff] [blame] | 2246 | void CastInst::anchor() {} |
| 2247 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2248 | // Just determine if this cast only deals with integral->integral conversion. |
| 2249 | bool CastInst::isIntegerCast() const { |
| 2250 | switch (getOpcode()) { |
| 2251 | default: return false; |
| 2252 | case Instruction::ZExt: |
| 2253 | case Instruction::SExt: |
| 2254 | case Instruction::Trunc: |
| 2255 | return true; |
| 2256 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2257 | return getOperand(0)->getType()->isIntegerTy() && |
| 2258 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2259 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2262 | bool CastInst::isLosslessCast() const { |
| 2263 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2264 | if (getOpcode() != Instruction::BitCast) |
| 2265 | return false; |
| 2266 | |
| 2267 | // Identity cast is always lossless |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2268 | Type* SrcTy = getOperand(0)->getType(); |
| 2269 | Type* DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2270 | if (SrcTy == DstTy) |
| 2271 | return true; |
| 2272 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2273 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2274 | if (SrcTy->isPointerTy()) |
| 2275 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2276 | return false; // Other types have no identity values |
| 2277 | } |
| 2278 | |
| 2279 | /// This function determines if the CastInst does not require any bits to be |
| 2280 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 2281 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 2282 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2283 | /// # bitcast i32* %x to i8* |
| 2284 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 2285 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2286 | /// @brief Determine if the described cast is a no-op. |
| 2287 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2288 | Type *SrcTy, |
| 2289 | Type *DestTy, |
| 2290 | Type *IntPtrTy) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2291 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2292 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2293 | case Instruction::Trunc: |
| 2294 | case Instruction::ZExt: |
| 2295 | case Instruction::SExt: |
| 2296 | case Instruction::FPTrunc: |
| 2297 | case Instruction::FPExt: |
| 2298 | case Instruction::UIToFP: |
| 2299 | case Instruction::SIToFP: |
| 2300 | case Instruction::FPToUI: |
| 2301 | case Instruction::FPToSI: |
| 2302 | return false; // These always modify bits |
| 2303 | case Instruction::BitCast: |
| 2304 | return true; // BitCast never modifies bits. |
| 2305 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2306 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2307 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2308 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2309 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2310 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2314 | /// @brief Determine if a cast is a no-op. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2315 | bool CastInst::isNoopCast(Type *IntPtrTy) const { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2316 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2317 | } |
| 2318 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2319 | /// This function determines if a pair of casts can be eliminated and what |
| 2320 | /// opcode should be used in the elimination. This assumes that there are two |
| 2321 | /// instructions like this: |
| 2322 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2323 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2324 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2325 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 2326 | /// If no such cast is permited, the function returns 0. |
| 2327 | unsigned CastInst::isEliminableCastPair( |
| 2328 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2329 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2330 | // Define the 144 possibilities for these two cast instructions. The values |
| 2331 | // in this matrix determine what to do in a given situation and select the |
| 2332 | // case in the switch below. The rows correspond to firstOp, the columns |
| 2333 | // correspond to secondOp. In looking at the table below, keep in mind |
| 2334 | // the following cast properties: |
| 2335 | // |
| 2336 | // Size Compare Source Destination |
| 2337 | // Operator Src ? Size Type Sign Type Sign |
| 2338 | // -------- ------------ ------------------- --------------------- |
| 2339 | // TRUNC > Integer Any Integral Any |
| 2340 | // ZEXT < Integral Unsigned Integer Any |
| 2341 | // SEXT < Integral Signed Integer Any |
| 2342 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 2343 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2344 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2345 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2346 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2347 | // FPEXT < FloatPt n/a FloatPt n/a |
| 2348 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2349 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Dan Gohman | eb7111b | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 2350 | // BITCAST = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2351 | // |
| 2352 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2353 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2354 | // into "fptoui double to i64", but this loses information about the range |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2355 | // of the produced value (we no longer know the top-part is all zeros). |
| 2356 | // Further this conversion is often much more expensive for typical hardware, |
| 2357 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 2358 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2359 | const unsigned numCastOps = |
| 2360 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2361 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 2362 | // T F F U S F F P I B -+ |
| 2363 | // R Z S P P I I T P 2 N T | |
| 2364 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 2365 | // N X X U S F F N X N 2 V | |
| 2366 | // C T T I I P P C T T P T -+ |
| 2367 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 2368 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 2369 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2370 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 2371 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2372 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 2373 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 2374 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 2375 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 2376 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 2377 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 2378 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 2379 | }; |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2380 | |
| 2381 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2382 | // merging. However, bitcast of A->B->A are allowed. |
| 2383 | bool isFirstBitcast = (firstOp == Instruction::BitCast); |
| 2384 | bool isSecondBitcast = (secondOp == Instruction::BitCast); |
| 2385 | bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast); |
| 2386 | |
| 2387 | // Check if any of the bitcasts convert scalars<->vectors. |
| 2388 | if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2389 | (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2390 | // Unless we are bitcasing to the original type, disallow optimizations. |
| 2391 | if (!chainedBitcast) return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2392 | |
| 2393 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2394 | [secondOp-Instruction::CastOpsBegin]; |
| 2395 | switch (ElimCase) { |
| 2396 | case 0: |
| 2397 | // categorically disallowed |
| 2398 | return 0; |
| 2399 | case 1: |
| 2400 | // allowed, use first cast's opcode |
| 2401 | return firstOp; |
| 2402 | case 2: |
| 2403 | // allowed, use second cast's opcode |
| 2404 | return secondOp; |
| 2405 | case 3: |
| 2406 | // no-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2407 | // is integer and we are not converting between a vector and a |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2408 | // non vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2409 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2410 | return firstOp; |
| 2411 | return 0; |
| 2412 | case 4: |
| 2413 | // no-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2414 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2415 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2416 | return firstOp; |
| 2417 | return 0; |
| 2418 | case 5: |
| 2419 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2420 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2421 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2422 | return secondOp; |
| 2423 | return 0; |
| 2424 | case 6: |
| 2425 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2426 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2427 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2428 | return secondOp; |
| 2429 | return 0; |
| 2430 | case 7: { |
| 2431 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2432 | if (!IntPtrTy) |
| 2433 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2434 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2435 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2436 | if (MidSize >= PtrSize) |
| 2437 | return Instruction::BitCast; |
| 2438 | return 0; |
| 2439 | } |
| 2440 | case 8: { |
| 2441 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2442 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2443 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2444 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2445 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2446 | if (SrcSize == DstSize) |
| 2447 | return Instruction::BitCast; |
| 2448 | else if (SrcSize < DstSize) |
| 2449 | return firstOp; |
| 2450 | return secondOp; |
| 2451 | } |
| 2452 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 2453 | return Instruction::ZExt; |
| 2454 | case 10: |
| 2455 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2456 | // the same as the original, in which case its just a bitcast |
| 2457 | if (SrcTy == DstTy) |
| 2458 | return Instruction::BitCast; |
| 2459 | return 0; // If the types are not the same we can't eliminate it. |
| 2460 | case 11: |
| 2461 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 2462 | // is a pointer to pointer cast. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2463 | if (SrcTy->isPointerTy() && MidTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2464 | return secondOp; |
| 2465 | return 0; |
| 2466 | case 12: |
| 2467 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2468 | if (MidTy->isPointerTy() && DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2469 | return firstOp; |
| 2470 | return 0; |
| 2471 | case 13: { |
| 2472 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2473 | if (!IntPtrTy) |
| 2474 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2475 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2476 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2477 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2478 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2479 | return Instruction::BitCast; |
| 2480 | return 0; |
| 2481 | } |
| 2482 | case 99: |
| 2483 | // cast combination can't happen (error in input). This is for all cases |
| 2484 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2485 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2486 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2487 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2488 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2491 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2492 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2493 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2494 | // Construct and return the appropriate CastInst subclass |
| 2495 | switch (op) { |
| 2496 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2497 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2498 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2499 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2500 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2501 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2502 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2503 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2504 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2505 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2506 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2507 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2508 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2509 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2512 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2513 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2514 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2515 | // Construct and return the appropriate CastInst subclass |
| 2516 | switch (op) { |
| 2517 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2518 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2519 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2520 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2521 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2522 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2523 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2524 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2525 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2526 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2527 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2528 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2529 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2530 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2533 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2534 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2535 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2536 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2537 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2538 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2541 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2542 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2543 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2544 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2545 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2546 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2549 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2550 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2551 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2552 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2553 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2554 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2557 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2558 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2559 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2560 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2561 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2562 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2565 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2566 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2567 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2568 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2569 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2570 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2573 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2574 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2575 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2576 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2577 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2578 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2581 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2582 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2583 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2584 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2585 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2586 | "Invalid cast"); |
| 2587 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2588 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2589 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2590 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2594 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2595 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2596 | Instruction *InsertBefore) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2597 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2598 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2599 | "Invalid cast"); |
| 2600 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2601 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2602 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2603 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2606 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2607 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2608 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2609 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2610 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2611 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2612 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2613 | Instruction::CastOps opcode = |
| 2614 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2615 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2616 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2617 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2620 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2621 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2622 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2623 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2624 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2625 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2626 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2627 | Instruction::CastOps opcode = |
| 2628 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2629 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2630 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2631 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2634 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2635 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2636 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2637 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2638 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2639 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2640 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2641 | Instruction::CastOps opcode = |
| 2642 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2643 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2644 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2647 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2648 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2649 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2650 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2651 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2652 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2653 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2654 | Instruction::CastOps opcode = |
| 2655 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2656 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2657 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2660 | // Check whether it is valid to call getCastOpcode for these types. |
| 2661 | // This routine must be kept in sync with getCastOpcode. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2662 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2663 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2664 | return false; |
| 2665 | |
| 2666 | if (SrcTy == DestTy) |
| 2667 | return true; |
| 2668 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2669 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2670 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2671 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2672 | // An element by element cast. Valid if casting the elements is valid. |
| 2673 | SrcTy = SrcVecTy->getElementType(); |
| 2674 | DestTy = DestVecTy->getElementType(); |
| 2675 | } |
| 2676 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2677 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2678 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2679 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2680 | |
| 2681 | // Run through the possibilities ... |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2682 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2683 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2684 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2685 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2686 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2687 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2688 | return DestBits == SrcBits; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2689 | } else { // Casting from something else |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2690 | return SrcTy->isPointerTy(); |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2691 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2692 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2693 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2694 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2695 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2696 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2697 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2698 | return DestBits == SrcBits; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2699 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2700 | return false; |
| 2701 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2702 | } else if (DestTy->isVectorTy()) { // Casting to vector |
| 2703 | return DestBits == SrcBits; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2704 | } else if (DestTy->isPointerTy()) { // Casting to pointer |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2705 | if (SrcTy->isPointerTy()) { // Casting from pointer |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2706 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2707 | } else if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2708 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2709 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2710 | return false; |
| 2711 | } |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2712 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2713 | if (SrcTy->isVectorTy()) { |
| 2714 | return DestBits == SrcBits; // 64-bit vector to MMX |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2715 | } else { |
| 2716 | return false; |
| 2717 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2718 | } else { // Casting to something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2719 | return false; |
| 2720 | } |
| 2721 | } |
| 2722 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2723 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2724 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2725 | // logic in the castIsValid function below. This axiom should hold: |
| 2726 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2727 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2728 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2729 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2730 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2731 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2732 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2733 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2734 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2735 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2736 | "Only first class types are castable!"); |
| 2737 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2738 | if (SrcTy == DestTy) |
| 2739 | return BitCast; |
| 2740 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2741 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2742 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2743 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2744 | // An element by element cast. Find the appropriate opcode based on the |
| 2745 | // element types. |
| 2746 | SrcTy = SrcVecTy->getElementType(); |
| 2747 | DestTy = DestVecTy->getElementType(); |
| 2748 | } |
| 2749 | |
| 2750 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2751 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2752 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2753 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2754 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2755 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2756 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2757 | if (DestBits < SrcBits) |
| 2758 | return Trunc; // int -> smaller int |
| 2759 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2760 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2761 | return SExt; // signed -> SEXT |
| 2762 | else |
| 2763 | return ZExt; // unsigned -> ZEXT |
| 2764 | } else { |
| 2765 | return BitCast; // Same size, No-op cast |
| 2766 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2767 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2768 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2769 | return FPToSI; // FP -> sint |
| 2770 | else |
| 2771 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2772 | } else if (SrcTy->isVectorTy()) { |
| 2773 | assert(DestBits == SrcBits && |
| 2774 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2775 | return BitCast; // Same size, no-op cast |
| 2776 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2777 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2778 | "Casting from a value that is not first-class type"); |
| 2779 | return PtrToInt; // ptr -> int |
| 2780 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2781 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2782 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2783 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2784 | return SIToFP; // sint -> FP |
| 2785 | else |
| 2786 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2787 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2788 | if (DestBits < SrcBits) { |
| 2789 | return FPTrunc; // FP -> smaller FP |
| 2790 | } else if (DestBits > SrcBits) { |
| 2791 | return FPExt; // FP -> larger FP |
| 2792 | } else { |
| 2793 | return BitCast; // same size, no-op cast |
| 2794 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2795 | } else if (SrcTy->isVectorTy()) { |
| 2796 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2797 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2798 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2799 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2800 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2801 | } else if (DestTy->isVectorTy()) { |
| 2802 | assert(DestBits == SrcBits && |
| 2803 | "Illegal cast to vector (wrong type or size)"); |
| 2804 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2805 | } else if (DestTy->isPointerTy()) { |
| 2806 | if (SrcTy->isPointerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2807 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2808 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2809 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2810 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2811 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2812 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2813 | if (SrcTy->isVectorTy()) { |
| 2814 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2815 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2816 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2817 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2818 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2819 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | //===----------------------------------------------------------------------===// |
| 2823 | // CastInst SubClass Constructors |
| 2824 | //===----------------------------------------------------------------------===// |
| 2825 | |
| 2826 | /// Check that the construction parameters for a CastInst are correct. This |
| 2827 | /// could be broken out into the separate constructors but it is useful to have |
| 2828 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2829 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2830 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2831 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2832 | |
| 2833 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2834 | Type *SrcTy = S->getType(); |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 2835 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 2836 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2837 | return false; |
| 2838 | |
| 2839 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2840 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2841 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2842 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2843 | // If these are vector types, get the lengths of the vectors (using zero for |
| 2844 | // scalar types means that checking that vector lengths match also checks that |
| 2845 | // scalars are not being converted to vectors or vectors to scalars). |
| 2846 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 2847 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 2848 | unsigned DstLength = DstTy->isVectorTy() ? |
| 2849 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 2850 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2851 | // Switch on the opcode provided |
| 2852 | switch (op) { |
| 2853 | default: return false; // This is an input error |
| 2854 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2855 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2856 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2857 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2858 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2859 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2860 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2861 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2862 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2863 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2864 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2865 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2866 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2867 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2868 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2869 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2870 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2871 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2872 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2873 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2874 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2875 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2876 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2877 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2878 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2879 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2880 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2881 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2882 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2883 | return SrcTy->getScalarType()->isPointerTy() && |
| 2884 | DstTy->getScalarType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2885 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2886 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2887 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2888 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2889 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2890 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2891 | return SrcTy->getScalarType()->isIntegerTy() && |
| 2892 | DstTy->getScalarType()->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2893 | case Instruction::BitCast: |
| 2894 | // BitCast implies a no-op cast of type only. No bits change. |
| 2895 | // However, you can't cast pointers to anything but pointers. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2896 | if (SrcTy->isPointerTy() != DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2897 | return false; |
| 2898 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2899 | // Now we know we're not dealing with a pointer/non-pointer mismatch. In all |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2900 | // these cases, the cast is okay if the source and destination bit widths |
| 2901 | // are identical. |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2902 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2907 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2908 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2909 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2913 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2914 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2915 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2919 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2920 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2921 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2925 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2926 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2927 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2928 | } |
| 2929 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2930 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2931 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2932 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2935 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2936 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2937 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2938 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2942 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2943 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2944 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2948 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2949 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2950 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2954 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2955 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2956 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2960 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2961 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2962 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2966 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2967 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2968 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2972 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2973 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2974 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2978 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2979 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2980 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2984 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2985 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2986 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2990 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2991 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2992 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2996 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2997 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2998 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3002 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3003 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3004 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3008 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3009 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3010 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3014 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3015 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3016 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3020 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3021 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3022 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3026 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3027 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3028 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3032 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3033 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3034 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3038 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3039 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3040 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3044 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3045 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3046 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3047 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3048 | |
| 3049 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3050 | // CmpInst Classes |
| 3051 | //===----------------------------------------------------------------------===// |
| 3052 | |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 3053 | void CmpInst::anchor() {} |
Chris Lattner | aec33da | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3055 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3056 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3057 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3058 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3059 | OperandTraits<CmpInst>::op_begin(this), |
| 3060 | OperandTraits<CmpInst>::operands(this), |
| 3061 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3062 | Op<0>() = LHS; |
| 3063 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3064 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3065 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3066 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3067 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3068 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3069 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3070 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3071 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3072 | OperandTraits<CmpInst>::op_begin(this), |
| 3073 | OperandTraits<CmpInst>::operands(this), |
| 3074 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3075 | Op<0>() = LHS; |
| 3076 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3077 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3078 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | CmpInst * |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3082 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3083 | Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3084 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3085 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3086 | if (InsertBefore) |
| 3087 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3088 | S1, S2, Name); |
| 3089 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3090 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3091 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3092 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3093 | |
| 3094 | if (InsertBefore) |
| 3095 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3096 | S1, S2, Name); |
| 3097 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3098 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3099 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3103 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3104 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3105 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3106 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3107 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3108 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3109 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3110 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | void CmpInst::swapOperands() { |
| 3114 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3115 | IC->swapOperands(); |
| 3116 | else |
| 3117 | cast<FCmpInst>(this)->swapOperands(); |
| 3118 | } |
| 3119 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3120 | bool CmpInst::isCommutative() const { |
| 3121 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3122 | return IC->isCommutative(); |
| 3123 | return cast<FCmpInst>(this)->isCommutative(); |
| 3124 | } |
| 3125 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3126 | bool CmpInst::isEquality() const { |
| 3127 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3128 | return IC->isEquality(); |
| 3129 | return cast<FCmpInst>(this)->isEquality(); |
| 3130 | } |
| 3131 | |
| 3132 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3133 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3134 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3135 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3136 | case ICMP_EQ: return ICMP_NE; |
| 3137 | case ICMP_NE: return ICMP_EQ; |
| 3138 | case ICMP_UGT: return ICMP_ULE; |
| 3139 | case ICMP_ULT: return ICMP_UGE; |
| 3140 | case ICMP_UGE: return ICMP_ULT; |
| 3141 | case ICMP_ULE: return ICMP_UGT; |
| 3142 | case ICMP_SGT: return ICMP_SLE; |
| 3143 | case ICMP_SLT: return ICMP_SGE; |
| 3144 | case ICMP_SGE: return ICMP_SLT; |
| 3145 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3146 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3147 | case FCMP_OEQ: return FCMP_UNE; |
| 3148 | case FCMP_ONE: return FCMP_UEQ; |
| 3149 | case FCMP_OGT: return FCMP_ULE; |
| 3150 | case FCMP_OLT: return FCMP_UGE; |
| 3151 | case FCMP_OGE: return FCMP_ULT; |
| 3152 | case FCMP_OLE: return FCMP_UGT; |
| 3153 | case FCMP_UEQ: return FCMP_ONE; |
| 3154 | case FCMP_UNE: return FCMP_OEQ; |
| 3155 | case FCMP_UGT: return FCMP_OLE; |
| 3156 | case FCMP_ULT: return FCMP_OGE; |
| 3157 | case FCMP_UGE: return FCMP_OLT; |
| 3158 | case FCMP_ULE: return FCMP_OGT; |
| 3159 | case FCMP_ORD: return FCMP_UNO; |
| 3160 | case FCMP_UNO: return FCMP_ORD; |
| 3161 | case FCMP_TRUE: return FCMP_FALSE; |
| 3162 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3166 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3167 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3168 | default: llvm_unreachable("Unknown icmp predicate!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3169 | case ICMP_EQ: case ICMP_NE: |
| 3170 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 3171 | return pred; |
| 3172 | case ICMP_UGT: return ICMP_SGT; |
| 3173 | case ICMP_ULT: return ICMP_SLT; |
| 3174 | case ICMP_UGE: return ICMP_SGE; |
| 3175 | case ICMP_ULE: return ICMP_SLE; |
| 3176 | } |
| 3177 | } |
| 3178 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3179 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3180 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3181 | default: llvm_unreachable("Unknown icmp predicate!"); |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3182 | case ICMP_EQ: case ICMP_NE: |
| 3183 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 3184 | return pred; |
| 3185 | case ICMP_SGT: return ICMP_UGT; |
| 3186 | case ICMP_SLT: return ICMP_ULT; |
| 3187 | case ICMP_SGE: return ICMP_UGE; |
| 3188 | case ICMP_SLE: return ICMP_ULE; |
| 3189 | } |
| 3190 | } |
| 3191 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3192 | /// Initialize a set of values that all satisfy the condition with C. |
| 3193 | /// |
| 3194 | ConstantRange |
| 3195 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 3196 | APInt Lower(C); |
| 3197 | APInt Upper(C); |
| 3198 | uint32_t BitWidth = C.getBitWidth(); |
| 3199 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3200 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3201 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 3202 | case ICmpInst::ICMP_NE: Lower++; break; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3203 | case ICmpInst::ICMP_ULT: |
| 3204 | Lower = APInt::getMinValue(BitWidth); |
| 3205 | // Check for an empty-set condition. |
| 3206 | if (Lower == Upper) |
| 3207 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3208 | break; |
| 3209 | case ICmpInst::ICMP_SLT: |
| 3210 | Lower = APInt::getSignedMinValue(BitWidth); |
| 3211 | // Check for an empty-set condition. |
| 3212 | if (Lower == Upper) |
| 3213 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3214 | break; |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3215 | case ICmpInst::ICMP_UGT: |
| 3216 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3217 | // Check for an empty-set condition. |
| 3218 | if (Lower == Upper) |
| 3219 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3220 | break; |
| 3221 | case ICmpInst::ICMP_SGT: |
| 3222 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3223 | // Check for an empty-set condition. |
| 3224 | if (Lower == Upper) |
| 3225 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3226 | break; |
| 3227 | case ICmpInst::ICMP_ULE: |
| 3228 | Lower = APInt::getMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3229 | // Check for a full-set condition. |
| 3230 | if (Lower == Upper) |
| 3231 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3232 | break; |
| 3233 | case ICmpInst::ICMP_SLE: |
| 3234 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3235 | // Check for a full-set condition. |
| 3236 | if (Lower == Upper) |
| 3237 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3238 | break; |
| 3239 | case ICmpInst::ICMP_UGE: |
| 3240 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3241 | // Check for a full-set condition. |
| 3242 | if (Lower == Upper) |
| 3243 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3244 | break; |
| 3245 | case ICmpInst::ICMP_SGE: |
| 3246 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3247 | // Check for a full-set condition. |
| 3248 | if (Lower == Upper) |
| 3249 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3250 | break; |
| 3251 | } |
| 3252 | return ConstantRange(Lower, Upper); |
| 3253 | } |
| 3254 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3255 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3256 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3257 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3258 | case ICMP_EQ: case ICMP_NE: |
| 3259 | return pred; |
| 3260 | case ICMP_SGT: return ICMP_SLT; |
| 3261 | case ICMP_SLT: return ICMP_SGT; |
| 3262 | case ICMP_SGE: return ICMP_SLE; |
| 3263 | case ICMP_SLE: return ICMP_SGE; |
| 3264 | case ICMP_UGT: return ICMP_ULT; |
| 3265 | case ICMP_ULT: return ICMP_UGT; |
| 3266 | case ICMP_UGE: return ICMP_ULE; |
| 3267 | case ICMP_ULE: return ICMP_UGE; |
| 3268 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3269 | case FCMP_FALSE: case FCMP_TRUE: |
| 3270 | case FCMP_OEQ: case FCMP_ONE: |
| 3271 | case FCMP_UEQ: case FCMP_UNE: |
| 3272 | case FCMP_ORD: case FCMP_UNO: |
| 3273 | return pred; |
| 3274 | case FCMP_OGT: return FCMP_OLT; |
| 3275 | case FCMP_OLT: return FCMP_OGT; |
| 3276 | case FCMP_OGE: return FCMP_OLE; |
| 3277 | case FCMP_OLE: return FCMP_OGE; |
| 3278 | case FCMP_UGT: return FCMP_ULT; |
| 3279 | case FCMP_ULT: return FCMP_UGT; |
| 3280 | case FCMP_UGE: return FCMP_ULE; |
| 3281 | case FCMP_ULE: return FCMP_UGE; |
| 3282 | } |
| 3283 | } |
| 3284 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3285 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 3286 | switch (predicate) { |
| 3287 | default: return false; |
| 3288 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 3289 | case ICmpInst::ICMP_UGE: return true; |
| 3290 | } |
| 3291 | } |
| 3292 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3293 | bool CmpInst::isSigned(unsigned short predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3294 | switch (predicate) { |
| 3295 | default: return false; |
| 3296 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 3297 | case ICmpInst::ICMP_SGE: return true; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 3302 | switch (predicate) { |
| 3303 | default: return false; |
| 3304 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3305 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 3306 | case FCmpInst::FCMP_ORD: return true; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 3311 | switch (predicate) { |
| 3312 | default: return false; |
| 3313 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3314 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 3315 | case FCmpInst::FCMP_UNO: return true; |
| 3316 | } |
| 3317 | } |
| 3318 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3319 | bool CmpInst::isTrueWhenEqual(unsigned short predicate) { |
| 3320 | switch(predicate) { |
| 3321 | default: return false; |
| 3322 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3323 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | bool CmpInst::isFalseWhenEqual(unsigned short predicate) { |
| 3328 | switch(predicate) { |
| 3329 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3330 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3331 | default: return false; |
| 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3336 | //===----------------------------------------------------------------------===// |
| 3337 | // SwitchInst Implementation |
| 3338 | //===----------------------------------------------------------------------===// |
| 3339 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3340 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3341 | assert(Value && Default && NumReserved); |
| 3342 | ReservedSpace = NumReserved; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3343 | NumOperands = 2; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3344 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3345 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3346 | OperandList[0] = Value; |
| 3347 | OperandList[1] = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3350 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3351 | /// switch on and a default destination. The number of additional cases can |
| 3352 | /// be specified here to make memory allocation more efficient. This |
| 3353 | /// constructor can also autoinsert before another instruction. |
| 3354 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3355 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3356 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3357 | 0, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3358 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3362 | /// switch on and a default destination. The number of additional cases can |
| 3363 | /// be specified here to make memory allocation more efficient. This |
| 3364 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3365 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3366 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3367 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3368 | 0, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3369 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3372 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3373 | : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) { |
| 3374 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
| 3375 | NumOperands = SI.getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3376 | Use *OL = OperandList, *InOL = SI.OperandList; |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3377 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3378 | OL[i] = InOL[i]; |
| 3379 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3380 | } |
Stepan Dyatkovskiy | a6c8cc3 | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3381 | TheSubsets = SI.TheSubsets; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3382 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 3385 | SwitchInst::~SwitchInst() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3386 | dropHungoffUses(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3390 | /// addCase - Add an entry to the switch instruction... |
| 3391 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3392 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3393 | IntegersSubsetToBB Mapping; |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 3394 | |
| 3395 | // FIXME: Currently we work with ConstantInt based cases. |
| 3396 | // So inititalize IntItem container directly from ConstantInt. |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3397 | Mapping.add(IntItem::fromConstantInt(OnVal)); |
| 3398 | IntegersSubset CaseRanges = Mapping.getCase(); |
| 3399 | addCase(CaseRanges, Dest); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3402 | void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) { |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3403 | unsigned NewCaseIdx = getNumCases(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3404 | unsigned OpNo = NumOperands; |
| 3405 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3406 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3407 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3408 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3409 | NumOperands = OpNo+2; |
Stepan Dyatkovskiy | a6c8cc3 | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3410 | |
| 3411 | SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal); |
| 3412 | |
| 3413 | CaseIt Case(this, NewCaseIdx, TheSubsetsIt); |
| 3414 | Case.updateCaseValueOperand(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3415 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3418 | /// removeCase - This method removes the specified case and its successor |
| 3419 | /// from the switch instruction. |
Stepan Dyatkovskiy | a6c8cc3 | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3420 | void SwitchInst::removeCase(CaseIt& i) { |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3421 | unsigned idx = i.getCaseIndex(); |
| 3422 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3423 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3424 | |
| 3425 | unsigned NumOps = getNumOperands(); |
| 3426 | Use *OL = OperandList; |
| 3427 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3428 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3429 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3430 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3431 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | // Nuke the last value. |
| 3435 | OL[NumOps-2].set(0); |
| 3436 | OL[NumOps-2+1].set(0); |
Stepan Dyatkovskiy | a6c8cc3 | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Do the same with TheCases collection: |
| 3439 | if (i.SubsetIt != --TheSubsets.end()) { |
| 3440 | *i.SubsetIt = TheSubsets.back(); |
| 3441 | TheSubsets.pop_back(); |
| 3442 | } else { |
| 3443 | TheSubsets.pop_back(); |
| 3444 | i.SubsetIt = TheSubsets.end(); |
| 3445 | } |
| 3446 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3447 | NumOperands = NumOps-2; |
| 3448 | } |
| 3449 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3450 | /// growOperands - grow operands - This grows the operand list in response |
| 3451 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3452 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3453 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3454 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3455 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3456 | |
| 3457 | ReservedSpace = NumOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3458 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3459 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3460 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3461 | NewOps[i] = OldOps[i]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3462 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3463 | OperandList = NewOps; |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3464 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | |
| 3468 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3469 | return getSuccessor(idx); |
| 3470 | } |
| 3471 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3472 | return getNumSuccessors(); |
| 3473 | } |
| 3474 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3475 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3476 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3477 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3478 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3479 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3480 | //===----------------------------------------------------------------------===// |
| 3481 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3482 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3483 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3484 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3485 | ReservedSpace = 1+NumDests; |
| 3486 | NumOperands = 1; |
| 3487 | OperandList = allocHungoffUses(ReservedSpace); |
| 3488 | |
| 3489 | OperandList[0] = Address; |
| 3490 | } |
| 3491 | |
| 3492 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3493 | /// growOperands - grow operands - This grows the operand list in response |
| 3494 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3495 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3496 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3497 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3498 | unsigned NumOps = e*2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3499 | |
| 3500 | ReservedSpace = NumOps; |
| 3501 | Use *NewOps = allocHungoffUses(NumOps); |
| 3502 | Use *OldOps = OperandList; |
| 3503 | for (unsigned i = 0; i != e; ++i) |
| 3504 | NewOps[i] = OldOps[i]; |
| 3505 | OperandList = NewOps; |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3506 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3509 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3510 | Instruction *InsertBefore) |
| 3511 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3512 | 0, 0, InsertBefore) { |
| 3513 | init(Address, NumCases); |
| 3514 | } |
| 3515 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3516 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3517 | BasicBlock *InsertAtEnd) |
| 3518 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3519 | 0, 0, InsertAtEnd) { |
| 3520 | init(Address, NumCases); |
| 3521 | } |
| 3522 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3523 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
| 3524 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3525 | allocHungoffUses(IBI.getNumOperands()), |
| 3526 | IBI.getNumOperands()) { |
| 3527 | Use *OL = OperandList, *InOL = IBI.OperandList; |
| 3528 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3529 | OL[i] = InOL[i]; |
| 3530 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3531 | } |
| 3532 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3533 | IndirectBrInst::~IndirectBrInst() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3534 | dropHungoffUses(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | /// addDestination - Add a destination. |
| 3538 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3539 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3540 | unsigned OpNo = NumOperands; |
| 3541 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3542 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3543 | // Initialize some new operands. |
| 3544 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 3545 | NumOperands = OpNo+1; |
| 3546 | OperandList[OpNo] = DestBB; |
| 3547 | } |
| 3548 | |
| 3549 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3550 | /// indirectbr instruction. |
| 3551 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3552 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3553 | |
| 3554 | unsigned NumOps = getNumOperands(); |
| 3555 | Use *OL = OperandList; |
| 3556 | |
| 3557 | // Replace this value with the last one. |
| 3558 | OL[idx+1] = OL[NumOps-1]; |
| 3559 | |
| 3560 | // Nuke the last value. |
| 3561 | OL[NumOps-1].set(0); |
| 3562 | NumOperands = NumOps-1; |
| 3563 | } |
| 3564 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3565 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3566 | return getSuccessor(idx); |
| 3567 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3568 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3569 | return getNumSuccessors(); |
| 3570 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3571 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3572 | setSuccessor(idx, B); |
| 3573 | } |
| 3574 | |
| 3575 | //===----------------------------------------------------------------------===// |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3576 | // clone_impl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3577 | //===----------------------------------------------------------------------===// |
| 3578 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3579 | // Define these methods here so vtables don't get emitted into every translation |
| 3580 | // unit that uses these classes. |
| 3581 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3582 | GetElementPtrInst *GetElementPtrInst::clone_impl() const { |
| 3583 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3586 | BinaryOperator *BinaryOperator::clone_impl() const { |
| 3587 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3590 | FCmpInst* FCmpInst::clone_impl() const { |
| 3591 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3594 | ICmpInst* ICmpInst::clone_impl() const { |
| 3595 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3598 | ExtractValueInst *ExtractValueInst::clone_impl() const { |
| 3599 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3602 | InsertValueInst *InsertValueInst::clone_impl() const { |
| 3603 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3606 | AllocaInst *AllocaInst::clone_impl() const { |
| 3607 | return new AllocaInst(getAllocatedType(), |
| 3608 | (Value*)getOperand(0), |
| 3609 | getAlignment()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3612 | LoadInst *LoadInst::clone_impl() const { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3613 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
| 3614 | getAlignment(), getOrdering(), getSynchScope()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3617 | StoreInst *StoreInst::clone_impl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3618 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3619 | getAlignment(), getOrdering(), getSynchScope()); |
| 3620 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3623 | AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const { |
| 3624 | AtomicCmpXchgInst *Result = |
| 3625 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
| 3626 | getOrdering(), getSynchScope()); |
| 3627 | Result->setVolatile(isVolatile()); |
| 3628 | return Result; |
| 3629 | } |
| 3630 | |
| 3631 | AtomicRMWInst *AtomicRMWInst::clone_impl() const { |
| 3632 | AtomicRMWInst *Result = |
| 3633 | new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), |
| 3634 | getOrdering(), getSynchScope()); |
| 3635 | Result->setVolatile(isVolatile()); |
| 3636 | return Result; |
| 3637 | } |
| 3638 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3639 | FenceInst *FenceInst::clone_impl() const { |
| 3640 | return new FenceInst(getContext(), getOrdering(), getSynchScope()); |
| 3641 | } |
| 3642 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3643 | TruncInst *TruncInst::clone_impl() const { |
| 3644 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3647 | ZExtInst *ZExtInst::clone_impl() const { |
| 3648 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3651 | SExtInst *SExtInst::clone_impl() const { |
| 3652 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3655 | FPTruncInst *FPTruncInst::clone_impl() const { |
| 3656 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3659 | FPExtInst *FPExtInst::clone_impl() const { |
| 3660 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3663 | UIToFPInst *UIToFPInst::clone_impl() const { |
| 3664 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3667 | SIToFPInst *SIToFPInst::clone_impl() const { |
| 3668 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3671 | FPToUIInst *FPToUIInst::clone_impl() const { |
| 3672 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3675 | FPToSIInst *FPToSIInst::clone_impl() const { |
| 3676 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3679 | PtrToIntInst *PtrToIntInst::clone_impl() const { |
| 3680 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3683 | IntToPtrInst *IntToPtrInst::clone_impl() const { |
| 3684 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3685 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3686 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3687 | BitCastInst *BitCastInst::clone_impl() const { |
| 3688 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3689 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3690 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3691 | CallInst *CallInst::clone_impl() const { |
| 3692 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3695 | SelectInst *SelectInst::clone_impl() const { |
| 3696 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3697 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3698 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3699 | VAArgInst *VAArgInst::clone_impl() const { |
| 3700 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3701 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3702 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3703 | ExtractElementInst *ExtractElementInst::clone_impl() const { |
| 3704 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3705 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3706 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3707 | InsertElementInst *InsertElementInst::clone_impl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3708 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3711 | ShuffleVectorInst *ShuffleVectorInst::clone_impl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3712 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3713 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3714 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3715 | PHINode *PHINode::clone_impl() const { |
| 3716 | return new PHINode(*this); |
| 3717 | } |
| 3718 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3719 | LandingPadInst *LandingPadInst::clone_impl() const { |
| 3720 | return new LandingPadInst(*this); |
| 3721 | } |
| 3722 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3723 | ReturnInst *ReturnInst::clone_impl() const { |
| 3724 | return new(getNumOperands()) ReturnInst(*this); |
| 3725 | } |
| 3726 | |
| 3727 | BranchInst *BranchInst::clone_impl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3728 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3729 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3730 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3731 | SwitchInst *SwitchInst::clone_impl() const { |
| 3732 | return new SwitchInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3735 | IndirectBrInst *IndirectBrInst::clone_impl() const { |
| 3736 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3740 | InvokeInst *InvokeInst::clone_impl() const { |
| 3741 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3742 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3743 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3744 | ResumeInst *ResumeInst::clone_impl() const { |
| 3745 | return new(1) ResumeInst(*this); |
| 3746 | } |
| 3747 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3748 | UnreachableInst *UnreachableInst::clone_impl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3749 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3750 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3751 | } |