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