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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 20 | #include "llvm/ParameterAttributes.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 26 | unsigned CallSite::getCallingConv() const { |
| 27 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 28 | return CI->getCallingConv(); |
| 29 | else |
| 30 | return cast<InvokeInst>(I)->getCallingConv(); |
| 31 | } |
| 32 | void CallSite::setCallingConv(unsigned CC) { |
| 33 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 34 | CI->setCallingConv(CC); |
| 35 | else |
| 36 | cast<InvokeInst>(I)->setCallingConv(CC); |
| 37 | } |
| 38 | |
| 39 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 40 | |
| 41 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 43 | // TerminatorInst Class |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 46 | // Out of line virtual method, so the vtable, etc has a home. |
| 47 | TerminatorInst::~TerminatorInst() { |
| 48 | } |
| 49 | |
| 50 | // Out of line virtual method, so the vtable, etc has a home. |
| 51 | UnaryInstruction::~UnaryInstruction() { |
| 52 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // PHINode Class |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | PHINode::PHINode(const PHINode &PN) |
| 60 | : Instruction(PN.getType(), Instruction::PHI, |
| 61 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 62 | ReservedSpace(PN.getNumOperands()) { |
| 63 | Use *OL = OperandList; |
| 64 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 65 | OL[i].init(PN.getOperand(i), this); |
| 66 | OL[i+1].init(PN.getOperand(i+1), this); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | PHINode::~PHINode() { |
| 71 | delete [] OperandList; |
| 72 | } |
| 73 | |
| 74 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 75 | // predecessor basic block is deleted. |
| 76 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 77 | unsigned NumOps = getNumOperands(); |
| 78 | Use *OL = OperandList; |
| 79 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 80 | Value *Removed = OL[Idx*2]; |
| 81 | |
| 82 | // Move everything after this operand down. |
| 83 | // |
| 84 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 85 | // client might not expect this to happen. The code as it is thrashes the |
| 86 | // use/def lists, which is kinda lame. |
| 87 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 88 | OL[i-2] = OL[i]; |
| 89 | OL[i-2+1] = OL[i+1]; |
| 90 | } |
| 91 | |
| 92 | // Nuke the last value. |
| 93 | OL[NumOps-2].set(0); |
| 94 | OL[NumOps-2+1].set(0); |
| 95 | NumOperands = NumOps-2; |
| 96 | |
| 97 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 98 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 99 | // If anyone is using this PHI, make them use a dummy value instead... |
| 100 | replaceAllUsesWith(UndefValue::get(getType())); |
| 101 | eraseFromParent(); |
| 102 | } |
| 103 | return Removed; |
| 104 | } |
| 105 | |
| 106 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 107 | /// list according to the following behavior: |
| 108 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 109 | /// of operation. This grows the number of ops by 1.5 times. |
| 110 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 111 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 112 | /// |
| 113 | void PHINode::resizeOperands(unsigned NumOps) { |
| 114 | if (NumOps == 0) { |
| 115 | NumOps = (getNumOperands())*3/2; |
| 116 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 117 | } else if (NumOps*2 > NumOperands) { |
| 118 | // No resize needed. |
| 119 | if (ReservedSpace >= NumOps) return; |
| 120 | } else if (NumOps == NumOperands) { |
| 121 | if (ReservedSpace == NumOps) return; |
| 122 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 123 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ReservedSpace = NumOps; |
| 127 | Use *NewOps = new Use[NumOps]; |
| 128 | Use *OldOps = OperandList; |
| 129 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 130 | NewOps[i].init(OldOps[i], this); |
| 131 | OldOps[i].set(0); |
| 132 | } |
| 133 | delete [] OldOps; |
| 134 | OperandList = NewOps; |
| 135 | } |
| 136 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 137 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 138 | /// value, return the value, otherwise return null. |
| 139 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 140 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 141 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 142 | if (getNumIncomingValues() == 1) |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 143 | if (getIncomingValue(0) != this) // not X = phi X |
| 144 | return getIncomingValue(0); |
| 145 | else |
| 146 | return UndefValue::get(getType()); // Self cycle is dead. |
| 147 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 148 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 149 | // the PHI node with the incoming value. |
| 150 | // |
| 151 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 152 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 153 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 154 | if (isa<UndefValue>(getIncomingValue(i))) |
| 155 | HasUndefInput = true; |
| 156 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 157 | if (InVal && getIncomingValue(i) != InVal) |
| 158 | return 0; // Not the same, bail out. |
| 159 | else |
| 160 | InVal = getIncomingValue(i); |
| 161 | |
| 162 | // The only case that could cause InVal to be null is if we have a PHI node |
| 163 | // that only has entries for itself. In this case, there is no entry into the |
| 164 | // loop, so kill the PHI. |
| 165 | // |
| 166 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 167 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 168 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 169 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 170 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 171 | // or if the client is prepared to deal with this possibility. |
| 172 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 173 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 174 | // If it's in the entry block, it dominates everything. |
Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 175 | if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 176 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 177 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 178 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 179 | // All of the incoming values are the same, return the value now. |
| 180 | return InVal; |
| 181 | } |
| 182 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 183 | |
| 184 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 185 | // CallInst Implementation |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 188 | CallInst::~CallInst() { |
| 189 | delete [] OperandList; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 190 | if (ParamAttrs) |
| 191 | ParamAttrs->dropRef(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 194 | void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 195 | ParamAttrs = 0; |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 196 | NumOperands = NumParams+1; |
| 197 | Use *OL = OperandList = new Use[NumParams+1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 198 | OL[0].init(Func, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 199 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 200 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 201 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 202 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 204 | assert((NumParams == FTy->getNumParams() || |
| 205 | (FTy->isVarArg() && NumParams > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 206 | "Calling a function with bad signature!"); |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 207 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 208 | assert((i >= FTy->getNumParams() || |
| 209 | FTy->getParamType(i) == Params[i]->getType()) && |
| 210 | "Calling a function with a bad signature!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 211 | OL[i+1].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 212 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 215 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 216 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 217 | NumOperands = 3; |
| 218 | Use *OL = OperandList = new Use[3]; |
| 219 | OL[0].init(Func, this); |
| 220 | OL[1].init(Actual1, this); |
| 221 | OL[2].init(Actual2, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 222 | |
| 223 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 224 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 225 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 226 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 227 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 228 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 229 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 230 | assert((0 >= FTy->getNumParams() || |
| 231 | FTy->getParamType(0) == Actual1->getType()) && |
| 232 | "Calling a function with a bad signature!"); |
| 233 | assert((1 >= FTy->getNumParams() || |
| 234 | FTy->getParamType(1) == Actual2->getType()) && |
| 235 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 238 | void CallInst::init(Value *Func, Value *Actual) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 239 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 240 | NumOperands = 2; |
| 241 | Use *OL = OperandList = new Use[2]; |
| 242 | OL[0].init(Func, this); |
| 243 | OL[1].init(Actual, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 244 | |
| 245 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 246 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 247 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 248 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 249 | assert((FTy->getNumParams() == 1 || |
| 250 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 251 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 252 | assert((0 == FTy->getNumParams() || |
| 253 | FTy->getParamType(0) == Actual->getType()) && |
| 254 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 257 | void CallInst::init(Value *Func) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 258 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 259 | NumOperands = 1; |
| 260 | Use *OL = OperandList = new Use[1]; |
| 261 | OL[0].init(Func, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 262 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 263 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 264 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 265 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 266 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 267 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 268 | } |
| 269 | |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 270 | #if 0 |
| 271 | // Leave for llvm-gcc |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 272 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 273 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 274 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 275 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 276 | Instruction::Call, 0, 0, InsertAtEnd) { |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 277 | init(Func, Args, NumArgs); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 278 | setName(Name); |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 279 | } |
| 280 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
| 281 | const std::string &Name, Instruction *InsertBefore) |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 282 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 283 | ->getElementType())->getReturnType(), |
| 284 | Instruction::Call, 0, 0, InsertBefore) { |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 285 | init(Func, Args, NumArgs); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 286 | setName(Name); |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 289 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 290 | const std::string &Name, Instruction *InsertBefore) |
| 291 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 292 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 293 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 294 | init(Func, Actual1, Actual2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 295 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 299 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 300 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 301 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 302 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 303 | init(Func, Actual1, Actual2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 304 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 305 | } |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 306 | #endif |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 307 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 308 | Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 309 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 310 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 311 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 313 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 317 | BasicBlock *InsertAtEnd) |
| 318 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 319 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 320 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 321 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 322 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 323 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 324 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 325 | Instruction *InsertBefore) |
| 326 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 327 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 328 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 329 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 330 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 334 | BasicBlock *InsertAtEnd) |
| 335 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 336 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 337 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 338 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 339 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 342 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 343 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
| 344 | CI.getNumOperands()) { |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 345 | ParamAttrs = 0; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 346 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 347 | Use *OL = OperandList; |
| 348 | Use *InOL = CI.OperandList; |
| 349 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 350 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 353 | void CallInst::setParamAttrs(ParamAttrsList *newAttrs) { |
| 354 | if (ParamAttrs) |
| 355 | ParamAttrs->dropRef(); |
| 356 | |
| 357 | if (newAttrs) |
| 358 | newAttrs->addRef(); |
| 359 | |
| 360 | ParamAttrs = newAttrs; |
| 361 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // InvokeInst Implementation |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 367 | InvokeInst::~InvokeInst() { |
| 368 | delete [] OperandList; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 369 | if (ParamAttrs) |
| 370 | ParamAttrs->dropRef(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 373 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 374 | Value* const *Args, unsigned NumArgs) { |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 375 | ParamAttrs = 0; |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 376 | NumOperands = 3+NumArgs; |
| 377 | Use *OL = OperandList = new Use[3+NumArgs]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 378 | OL[0].init(Fn, this); |
| 379 | OL[1].init(IfNormal, this); |
| 380 | OL[2].init(IfException, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 381 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 382 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 383 | FTy = FTy; // silence warning. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 384 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 385 | assert((NumArgs == FTy->getNumParams()) || |
| 386 | (FTy->isVarArg() && NumArgs > FTy->getNumParams()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 387 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 388 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 389 | for (unsigned i = 0, e = NumArgs; i != e; i++) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 390 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 391 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 392 | "Invoking a function with a bad signature!"); |
| 393 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 394 | OL[i+3].init(Args[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 395 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 398 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 399 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 400 | new Use[II.getNumOperands()], II.getNumOperands()) { |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 401 | ParamAttrs = 0; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 402 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 403 | Use *OL = OperandList, *InOL = II.OperandList; |
| 404 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 405 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 408 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 409 | return getSuccessor(idx); |
| 410 | } |
| 411 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 412 | return getNumSuccessors(); |
| 413 | } |
| 414 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 415 | return setSuccessor(idx, B); |
| 416 | } |
| 417 | |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 418 | void InvokeInst::setParamAttrs(ParamAttrsList *newAttrs) { |
| 419 | if (ParamAttrs) |
| 420 | ParamAttrs->dropRef(); |
| 421 | |
| 422 | if (newAttrs) |
| 423 | newAttrs->addRef(); |
| 424 | |
| 425 | ParamAttrs = newAttrs; |
| 426 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 427 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | // ReturnInst Implementation |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 432 | ReturnInst::ReturnInst(const ReturnInst &RI) |
| 433 | : TerminatorInst(Type::VoidTy, Instruction::Ret, |
| 434 | &RetVal, RI.getNumOperands()) { |
| 435 | if (RI.getNumOperands()) |
| 436 | RetVal.init(RI.RetVal, this); |
| 437 | } |
| 438 | |
| 439 | ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) |
| 440 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) { |
| 441 | init(retVal); |
| 442 | } |
| 443 | ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) |
| 444 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
| 445 | init(retVal); |
| 446 | } |
| 447 | ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) |
| 448 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
| 449 | } |
| 450 | |
| 451 | |
| 452 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 453 | void ReturnInst::init(Value *retVal) { |
| 454 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 455 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 456 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 457 | NumOperands = 1; |
| 458 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 462 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 463 | return getNumSuccessors(); |
| 464 | } |
| 465 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 466 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 467 | // emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 468 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 469 | assert(0 && "ReturnInst has no successors!"); |
| 470 | } |
| 471 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 472 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 473 | assert(0 && "ReturnInst has no successors!"); |
| 474 | abort(); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 479 | //===----------------------------------------------------------------------===// |
| 480 | // UnwindInst Implementation |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 483 | UnwindInst::UnwindInst(Instruction *InsertBefore) |
| 484 | : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) { |
| 485 | } |
| 486 | UnwindInst::UnwindInst(BasicBlock *InsertAtEnd) |
| 487 | : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) { |
| 488 | } |
| 489 | |
| 490 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 491 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 492 | return getNumSuccessors(); |
| 493 | } |
| 494 | |
| 495 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 496 | assert(0 && "UnwindInst has no successors!"); |
| 497 | } |
| 498 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 499 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 500 | assert(0 && "UnwindInst has no successors!"); |
| 501 | abort(); |
| 502 | return 0; |
| 503 | } |
| 504 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 505 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 506 | // UnreachableInst Implementation |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 509 | UnreachableInst::UnreachableInst(Instruction *InsertBefore) |
| 510 | : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) { |
| 511 | } |
| 512 | UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd) |
| 513 | : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) { |
| 514 | } |
| 515 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 516 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 517 | return getNumSuccessors(); |
| 518 | } |
| 519 | |
| 520 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 521 | assert(0 && "UnwindInst has no successors!"); |
| 522 | } |
| 523 | |
| 524 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 525 | assert(0 && "UnwindInst has no successors!"); |
| 526 | abort(); |
| 527 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 531 | // BranchInst Implementation |
| 532 | //===----------------------------------------------------------------------===// |
| 533 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 534 | void BranchInst::AssertOK() { |
| 535 | if (isConditional()) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 536 | assert(getCondition()->getType() == Type::Int1Ty && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 537 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 540 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
| 541 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) { |
| 542 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 543 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 544 | } |
| 545 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 546 | Instruction *InsertBefore) |
| 547 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) { |
| 548 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 549 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 550 | Ops[2].init(Cond, this); |
| 551 | #ifndef NDEBUG |
| 552 | AssertOK(); |
| 553 | #endif |
| 554 | } |
| 555 | |
| 556 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
| 557 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) { |
| 558 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 559 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 560 | } |
| 561 | |
| 562 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 563 | BasicBlock *InsertAtEnd) |
| 564 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) { |
| 565 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 566 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 567 | Ops[2].init(Cond, this); |
| 568 | #ifndef NDEBUG |
| 569 | AssertOK(); |
| 570 | #endif |
| 571 | } |
| 572 | |
| 573 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 574 | BranchInst::BranchInst(const BranchInst &BI) : |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 575 | TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 576 | OperandList[0].init(BI.getOperand(0), this); |
| 577 | if (BI.getNumOperands() != 1) { |
| 578 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 579 | OperandList[1].init(BI.getOperand(1), this); |
| 580 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 584 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 585 | return getSuccessor(idx); |
| 586 | } |
| 587 | unsigned BranchInst::getNumSuccessorsV() const { |
| 588 | return getNumSuccessors(); |
| 589 | } |
| 590 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 591 | setSuccessor(idx, B); |
| 592 | } |
| 593 | |
| 594 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 595 | //===----------------------------------------------------------------------===// |
| 596 | // AllocationInst Implementation |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 599 | static Value *getAISize(Value *Amt) { |
| 600 | if (!Amt) |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 601 | Amt = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 602 | else { |
| 603 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 604 | "Passed basic block into allocation size parameter! Use other ctor"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 605 | assert(Amt->getType() == Type::Int32Ty && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 606 | "Malloc/Allocation array size is not a 32-bit integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 607 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 608 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 611 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 612 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 613 | Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 614 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 615 | InsertBefore), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 616 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 617 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 618 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 621 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 622 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 623 | BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 624 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 625 | InsertAtEnd), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 626 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 627 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 628 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 631 | // Out of line virtual method, so the vtable, etc has a home. |
| 632 | AllocationInst::~AllocationInst() { |
| 633 | } |
| 634 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 635 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 636 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
| 637 | return CI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 638 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | const Type *AllocationInst::getAllocatedType() const { |
| 642 | return getType()->getElementType(); |
| 643 | } |
| 644 | |
| 645 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 646 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 647 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | MallocInst::MallocInst(const MallocInst &MI) |
| 651 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 652 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | //===----------------------------------------------------------------------===// |
| 656 | // FreeInst Implementation |
| 657 | //===----------------------------------------------------------------------===// |
| 658 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 659 | void FreeInst::AssertOK() { |
| 660 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 661 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 665 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 666 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 670 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 671 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | // LoadInst Implementation |
| 677 | //===----------------------------------------------------------------------===// |
| 678 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 679 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 680 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 681 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 685 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 686 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 687 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 688 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 689 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 690 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 694 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 695 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 696 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 697 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 698 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 699 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 703 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 704 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 705 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 706 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 707 | setAlignment(0); |
| 708 | AssertOK(); |
| 709 | setName(Name); |
| 710 | } |
| 711 | |
| 712 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 713 | unsigned Align, Instruction *InsertBef) |
| 714 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 715 | Load, Ptr, InsertBef) { |
| 716 | setVolatile(isVolatile); |
| 717 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 718 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 719 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 722 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 723 | unsigned Align, BasicBlock *InsertAE) |
| 724 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 725 | Load, Ptr, InsertAE) { |
| 726 | setVolatile(isVolatile); |
| 727 | setAlignment(Align); |
| 728 | AssertOK(); |
| 729 | setName(Name); |
| 730 | } |
| 731 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 732 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 733 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 734 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 735 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 736 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 737 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 738 | AssertOK(); |
| 739 | setName(Name); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | |
| 744 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 745 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 746 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 747 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 748 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 749 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 750 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 754 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 755 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 756 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 757 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 758 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 759 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 763 | Instruction *InsertBef) |
| 764 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 765 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 766 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 767 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 768 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 769 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 773 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 774 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 775 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 776 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 777 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 778 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 779 | if (Name && Name[0]) setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 782 | void LoadInst::setAlignment(unsigned Align) { |
| 783 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 784 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 785 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 786 | |
| 787 | //===----------------------------------------------------------------------===// |
| 788 | // StoreInst Implementation |
| 789 | //===----------------------------------------------------------------------===// |
| 790 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 791 | void StoreInst::AssertOK() { |
| 792 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 793 | "Ptr must have pointer type!"); |
| 794 | assert(getOperand(0)->getType() == |
| 795 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 796 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 797 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 798 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 799 | |
| 800 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 801 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 802 | Ops[0].init(val, this); |
| 803 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 804 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 805 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 806 | AssertOK(); |
| 807 | } |
| 808 | |
| 809 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 810 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 811 | Ops[0].init(val, this); |
| 812 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 813 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 814 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 815 | AssertOK(); |
| 816 | } |
| 817 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 818 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 819 | Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 820 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 821 | Ops[0].init(val, this); |
| 822 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 823 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 824 | setAlignment(0); |
| 825 | AssertOK(); |
| 826 | } |
| 827 | |
| 828 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 829 | unsigned Align, Instruction *InsertBefore) |
| 830 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
| 831 | Ops[0].init(val, this); |
| 832 | Ops[1].init(addr, this); |
| 833 | setVolatile(isVolatile); |
| 834 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 835 | AssertOK(); |
| 836 | } |
| 837 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 838 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 839 | unsigned Align, BasicBlock *InsertAtEnd) |
| 840 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
| 841 | Ops[0].init(val, this); |
| 842 | Ops[1].init(addr, this); |
| 843 | setVolatile(isVolatile); |
| 844 | setAlignment(Align); |
| 845 | AssertOK(); |
| 846 | } |
| 847 | |
| 848 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 849 | BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 850 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 851 | Ops[0].init(val, this); |
| 852 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 853 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 854 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 855 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 858 | void StoreInst::setAlignment(unsigned Align) { |
| 859 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 860 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 861 | } |
| 862 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 863 | //===----------------------------------------------------------------------===// |
| 864 | // GetElementPtrInst Implementation |
| 865 | //===----------------------------------------------------------------------===// |
| 866 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 867 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { |
| 868 | NumOperands = 1+NumIdx; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 869 | Use *OL = OperandList = new Use[NumOperands]; |
| 870 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 872 | for (unsigned i = 0; i != NumIdx; ++i) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 873 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 876 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 877 | NumOperands = 2; |
| 878 | Use *OL = OperandList = new Use[2]; |
| 879 | OL[0].init(Ptr, this); |
| 880 | OL[1].init(Idx, this); |
| 881 | } |
| 882 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 883 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 884 | const std::string &Name, Instruction *InBe) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 885 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 886 | GetElementPtr, 0, 0, InBe) { |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 887 | init(Ptr, Idx); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 888 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 892 | const std::string &Name, BasicBlock *IAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 893 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 894 | GetElementPtr, 0, 0, IAE) { |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 895 | init(Ptr, Idx); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 896 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 899 | GetElementPtrInst::~GetElementPtrInst() { |
| 900 | delete[] OperandList; |
| 901 | } |
| 902 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 903 | // getIndexedType - Returns the type of the element that would be loaded with |
| 904 | // a load instruction with the specified parameters. |
| 905 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 906 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 907 | // pointer type. |
| 908 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 909 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 910 | Value* const *Idxs, |
| 911 | unsigned NumIdx, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 912 | bool AllowCompositeLeaf) { |
| 913 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 914 | |
| 915 | // Handle the special case of the empty set index set... |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 916 | if (NumIdx == 0) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 917 | if (AllowCompositeLeaf || |
| 918 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 919 | return cast<PointerType>(Ptr)->getElementType(); |
| 920 | else |
| 921 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 922 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 923 | unsigned CurIdx = 0; |
| 924 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 925 | if (NumIdx == CurIdx) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 926 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 927 | return 0; // Can't load a whole structure or array!?!? |
| 928 | } |
| 929 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 930 | Value *Index = Idxs[CurIdx++]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 931 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 932 | return 0; // Can only index into pointer types at the first index! |
| 933 | if (!CT->indexValid(Index)) return 0; |
| 934 | Ptr = CT->getTypeAtIndex(Index); |
| 935 | |
| 936 | // If the new type forwards to another type, then it is in the middle |
| 937 | // of being refined to another type (and hence, may have dropped all |
| 938 | // references to what it was using before). So, use the new forwarded |
| 939 | // type. |
| 940 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 941 | Ptr = Ty; |
| 942 | } |
| 943 | } |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 944 | return CurIdx == NumIdx ? Ptr : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 947 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 948 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 949 | if (!PTy) return 0; // Type isn't a pointer type! |
| 950 | |
| 951 | // Check the pointer index. |
| 952 | if (!PTy->indexValid(Idx)) return 0; |
| 953 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 954 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 957 | |
| 958 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 959 | /// zeros. If so, the result pointer and the first operand have the same |
| 960 | /// value, just potentially different types. |
| 961 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 962 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 963 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 964 | if (!CI->isZero()) return false; |
| 965 | } else { |
| 966 | return false; |
| 967 | } |
| 968 | } |
| 969 | return true; |
| 970 | } |
| 971 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 972 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 973 | /// constant integers. If so, the result pointer and the first operand have |
| 974 | /// a constant offset between them. |
| 975 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 976 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 977 | if (!isa<ConstantInt>(getOperand(i))) |
| 978 | return false; |
| 979 | } |
| 980 | return true; |
| 981 | } |
| 982 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 983 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 984 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 985 | // ExtractElementInst Implementation |
| 986 | //===----------------------------------------------------------------------===// |
| 987 | |
| 988 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 989 | const std::string &Name, |
| 990 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 991 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 992 | ExtractElement, Ops, 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 993 | assert(isValidOperands(Val, Index) && |
| 994 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 995 | Ops[0].init(Val, this); |
| 996 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 997 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1000 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1001 | const std::string &Name, |
| 1002 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1003 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1004 | ExtractElement, Ops, 2, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1005 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1006 | assert(isValidOperands(Val, Index) && |
| 1007 | "Invalid extractelement instruction operands!"); |
| 1008 | Ops[0].init(Val, this); |
| 1009 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1010 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1014 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1015 | const std::string &Name, |
| 1016 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1017 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1018 | ExtractElement, Ops, 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1019 | assert(isValidOperands(Val, Index) && |
| 1020 | "Invalid extractelement instruction operands!"); |
| 1021 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1022 | Ops[0].init(Val, this); |
| 1023 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1024 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1027 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1028 | const std::string &Name, |
| 1029 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1030 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1031 | ExtractElement, Ops, 2, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1032 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1033 | assert(isValidOperands(Val, Index) && |
| 1034 | "Invalid extractelement instruction operands!"); |
| 1035 | |
| 1036 | Ops[0].init(Val, this); |
| 1037 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1038 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1042 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1043 | if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1044 | return false; |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1049 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1050 | // InsertElementInst Implementation |
| 1051 | //===----------------------------------------------------------------------===// |
| 1052 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1053 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 1054 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 1055 | Ops[0].init(IE.Ops[0], this); |
| 1056 | Ops[1].init(IE.Ops[1], this); |
| 1057 | Ops[2].init(IE.Ops[2], this); |
| 1058 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1059 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1060 | const std::string &Name, |
| 1061 | Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1062 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1063 | assert(isValidOperands(Vec, Elt, Index) && |
| 1064 | "Invalid insertelement instruction operands!"); |
| 1065 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1066 | Ops[1].init(Elt, this); |
| 1067 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1068 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1071 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1072 | const std::string &Name, |
| 1073 | Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1074 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1075 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1076 | assert(isValidOperands(Vec, Elt, Index) && |
| 1077 | "Invalid insertelement instruction operands!"); |
| 1078 | Ops[0].init(Vec, this); |
| 1079 | Ops[1].init(Elt, this); |
| 1080 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1081 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1085 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1086 | const std::string &Name, |
| 1087 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1088 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1089 | assert(isValidOperands(Vec, Elt, Index) && |
| 1090 | "Invalid insertelement instruction operands!"); |
| 1091 | |
| 1092 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1093 | Ops[1].init(Elt, this); |
| 1094 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1095 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1098 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1099 | const std::string &Name, |
| 1100 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1101 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1102 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1103 | assert(isValidOperands(Vec, Elt, Index) && |
| 1104 | "Invalid insertelement instruction operands!"); |
| 1105 | |
| 1106 | Ops[0].init(Vec, this); |
| 1107 | Ops[1].init(Elt, this); |
| 1108 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1109 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1112 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1113 | const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1114 | if (!isa<VectorType>(Vec->getType())) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1115 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1116 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1117 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1118 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1119 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1120 | if (Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1121 | return false; // Third operand of insertelement must be uint. |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1126 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1127 | // ShuffleVectorInst Implementation |
| 1128 | //===----------------------------------------------------------------------===// |
| 1129 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1130 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 1131 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 1132 | Ops[0].init(SV.Ops[0], this); |
| 1133 | Ops[1].init(SV.Ops[1], this); |
| 1134 | Ops[2].init(SV.Ops[2], this); |
| 1135 | } |
| 1136 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1137 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1138 | const std::string &Name, |
| 1139 | Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1140 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1141 | assert(isValidOperands(V1, V2, Mask) && |
| 1142 | "Invalid shuffle vector instruction operands!"); |
| 1143 | Ops[0].init(V1, this); |
| 1144 | Ops[1].init(V2, this); |
| 1145 | Ops[2].init(Mask, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1146 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1150 | const std::string &Name, |
| 1151 | BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1152 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1153 | assert(isValidOperands(V1, V2, Mask) && |
| 1154 | "Invalid shuffle vector instruction operands!"); |
| 1155 | |
| 1156 | Ops[0].init(V1, this); |
| 1157 | Ops[1].init(V2, this); |
| 1158 | Ops[2].init(Mask, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1159 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1163 | const Value *Mask) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1164 | if (!isa<VectorType>(V1->getType())) return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1165 | if (V1->getType() != V2->getType()) return false; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1166 | if (!isa<VectorType>(Mask->getType()) || |
| 1167 | cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty || |
| 1168 | cast<VectorType>(Mask->getType())->getNumElements() != |
| 1169 | cast<VectorType>(V1->getType())->getNumElements()) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1170 | return false; |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1176 | // BinaryOperator Class |
| 1177 | //===----------------------------------------------------------------------===// |
| 1178 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1179 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1180 | const Type *Ty, const std::string &Name, |
| 1181 | Instruction *InsertBefore) |
| 1182 | : Instruction(Ty, iType, Ops, 2, InsertBefore) { |
| 1183 | Ops[0].init(S1, this); |
| 1184 | Ops[1].init(S2, this); |
| 1185 | init(iType); |
| 1186 | setName(Name); |
| 1187 | } |
| 1188 | |
| 1189 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1190 | const Type *Ty, const std::string &Name, |
| 1191 | BasicBlock *InsertAtEnd) |
| 1192 | : Instruction(Ty, iType, Ops, 2, InsertAtEnd) { |
| 1193 | Ops[0].init(S1, this); |
| 1194 | Ops[1].init(S2, this); |
| 1195 | init(iType); |
| 1196 | setName(Name); |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1201 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1202 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1203 | assert(LHS->getType() == RHS->getType() && |
| 1204 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1205 | #ifndef NDEBUG |
| 1206 | switch (iType) { |
| 1207 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1208 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1209 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1210 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1211 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1212 | isa<VectorType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1213 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1214 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1215 | case UDiv: |
| 1216 | case SDiv: |
| 1217 | assert(getType() == LHS->getType() && |
| 1218 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1219 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1220 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1221 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1222 | break; |
| 1223 | case FDiv: |
| 1224 | assert(getType() == LHS->getType() && |
| 1225 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1226 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1227 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1228 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1229 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1230 | case URem: |
| 1231 | case SRem: |
| 1232 | assert(getType() == LHS->getType() && |
| 1233 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1234 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1235 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1236 | "Incorrect operand type (not integer) for S/UREM"); |
| 1237 | break; |
| 1238 | case FRem: |
| 1239 | assert(getType() == LHS->getType() && |
| 1240 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1241 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1242 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1243 | && "Incorrect operand type (not floating point) for FREM"); |
| 1244 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1245 | case Shl: |
| 1246 | case LShr: |
| 1247 | case AShr: |
| 1248 | assert(getType() == LHS->getType() && |
| 1249 | "Shift operation should return same type as operands!"); |
| 1250 | assert(getType()->isInteger() && |
| 1251 | "Shift operation requires integer operands"); |
| 1252 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1253 | case And: case Or: |
| 1254 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1255 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1256 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1257 | assert((getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1258 | (isa<VectorType>(getType()) && |
| 1259 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1260 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1261 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1262 | default: |
| 1263 | break; |
| 1264 | } |
| 1265 | #endif |
| 1266 | } |
| 1267 | |
| 1268 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1269 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1270 | Instruction *InsertBefore) { |
| 1271 | assert(S1->getType() == S2->getType() && |
| 1272 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1273 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1277 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1278 | BasicBlock *InsertAtEnd) { |
| 1279 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1280 | InsertAtEnd->getInstList().push_back(Res); |
| 1281 | return Res; |
| 1282 | } |
| 1283 | |
| 1284 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1285 | Instruction *InsertBefore) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1286 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1287 | return new BinaryOperator(Instruction::Sub, |
| 1288 | zero, Op, |
| 1289 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1293 | BasicBlock *InsertAtEnd) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1294 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1295 | return new BinaryOperator(Instruction::Sub, |
| 1296 | zero, Op, |
| 1297 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1301 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1302 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1303 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1304 | C = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1305 | C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1306 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1307 | C = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1311 | Op->getType(), Name, InsertBefore); |
| 1312 | } |
| 1313 | |
| 1314 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1315 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1316 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1317 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1318 | // Create a vector of all ones values. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1319 | Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1320 | AllOnes = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1321 | ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1322 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1323 | AllOnes = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1327 | Op->getType(), Name, InsertAtEnd); |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | // isConstantAllOnes - Helper function for several functions below |
| 1332 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1333 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1334 | return CI->isAllOnesValue(); |
| 1335 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1336 | return CV->isAllOnesValue(); |
| 1337 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | bool BinaryOperator::isNeg(const Value *V) { |
| 1341 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1342 | if (Bop->getOpcode() == Instruction::Sub) |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1343 | return Bop->getOperand(0) == |
| 1344 | ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | bool BinaryOperator::isNot(const Value *V) { |
| 1349 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1350 | return (Bop->getOpcode() == Instruction::Xor && |
| 1351 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1352 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1356 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1357 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1358 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1361 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1362 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1365 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1366 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1367 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1368 | Value *Op0 = BO->getOperand(0); |
| 1369 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1370 | if (isConstantAllOnes(Op0)) return Op1; |
| 1371 | |
| 1372 | assert(isConstantAllOnes(Op1)); |
| 1373 | return Op0; |
| 1374 | } |
| 1375 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1376 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1377 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | // swapOperands - Exchange the two operands to this instruction. This |
| 1382 | // instruction is safe to use on any binary instruction and does not |
| 1383 | // modify the semantics of the instruction. If the instruction is |
| 1384 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1385 | // |
| 1386 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1387 | if (!isCommutative()) |
| 1388 | return true; // Can't commute operands |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1389 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1390 | return false; |
| 1391 | } |
| 1392 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1393 | //===----------------------------------------------------------------------===// |
| 1394 | // CastInst Class |
| 1395 | //===----------------------------------------------------------------------===// |
| 1396 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1397 | // Just determine if this cast only deals with integral->integral conversion. |
| 1398 | bool CastInst::isIntegerCast() const { |
| 1399 | switch (getOpcode()) { |
| 1400 | default: return false; |
| 1401 | case Instruction::ZExt: |
| 1402 | case Instruction::SExt: |
| 1403 | case Instruction::Trunc: |
| 1404 | return true; |
| 1405 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1406 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1407 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1410 | bool CastInst::isLosslessCast() const { |
| 1411 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1412 | if (getOpcode() != Instruction::BitCast) |
| 1413 | return false; |
| 1414 | |
| 1415 | // Identity cast is always lossless |
| 1416 | const Type* SrcTy = getOperand(0)->getType(); |
| 1417 | const Type* DstTy = getType(); |
| 1418 | if (SrcTy == DstTy) |
| 1419 | return true; |
| 1420 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1421 | // Pointer to pointer is always lossless. |
| 1422 | if (isa<PointerType>(SrcTy)) |
| 1423 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1424 | return false; // Other types have no identity values |
| 1425 | } |
| 1426 | |
| 1427 | /// This function determines if the CastInst does not require any bits to be |
| 1428 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1429 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1430 | /// example, the following are all no-op casts: |
| 1431 | /// # bitcast uint %X, int |
| 1432 | /// # bitcast uint* %x, sbyte* |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1433 | /// # bitcast vector< 2 x int > %x, vector< 4 x short> |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1434 | /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only |
| 1435 | /// @brief Determine if a cast is a no-op. |
| 1436 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1437 | switch (getOpcode()) { |
| 1438 | default: |
| 1439 | assert(!"Invalid CastOp"); |
| 1440 | case Instruction::Trunc: |
| 1441 | case Instruction::ZExt: |
| 1442 | case Instruction::SExt: |
| 1443 | case Instruction::FPTrunc: |
| 1444 | case Instruction::FPExt: |
| 1445 | case Instruction::UIToFP: |
| 1446 | case Instruction::SIToFP: |
| 1447 | case Instruction::FPToUI: |
| 1448 | case Instruction::FPToSI: |
| 1449 | return false; // These always modify bits |
| 1450 | case Instruction::BitCast: |
| 1451 | return true; // BitCast never modifies bits. |
| 1452 | case Instruction::PtrToInt: |
| 1453 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1454 | getType()->getPrimitiveSizeInBits(); |
| 1455 | case Instruction::IntToPtr: |
| 1456 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1457 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /// This function determines if a pair of casts can be eliminated and what |
| 1462 | /// opcode should be used in the elimination. This assumes that there are two |
| 1463 | /// instructions like this: |
| 1464 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1465 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1466 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1467 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1468 | /// If no such cast is permited, the function returns 0. |
| 1469 | unsigned CastInst::isEliminableCastPair( |
| 1470 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1471 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1472 | { |
| 1473 | // Define the 144 possibilities for these two cast instructions. The values |
| 1474 | // in this matrix determine what to do in a given situation and select the |
| 1475 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1476 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1477 | // the following cast properties: |
| 1478 | // |
| 1479 | // Size Compare Source Destination |
| 1480 | // Operator Src ? Size Type Sign Type Sign |
| 1481 | // -------- ------------ ------------------- --------------------- |
| 1482 | // TRUNC > Integer Any Integral Any |
| 1483 | // ZEXT < Integral Unsigned Integer Any |
| 1484 | // SEXT < Integral Signed Integer Any |
| 1485 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1486 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1487 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1488 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1489 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1490 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1491 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1492 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1493 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1494 | // |
| 1495 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
| 1496 | // For example, we could merge "fptoui double to uint" + "zext uint to ulong", |
| 1497 | // into "fptoui double to ulong", but this loses information about the range |
| 1498 | // of the produced value (we no longer know the top-part is all zeros). |
| 1499 | // Further this conversion is often much more expensive for typical hardware, |
| 1500 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1501 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1502 | const unsigned numCastOps = |
| 1503 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1504 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1505 | // T F F U S F F P I B -+ |
| 1506 | // R Z S P P I I T P 2 N T | |
| 1507 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1508 | // N X X U S F F N X N 2 V | |
| 1509 | // C T T I I P P C T T P T -+ |
| 1510 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1511 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1512 | { 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] | 1513 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1514 | { 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] | 1515 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1516 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1517 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1518 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1519 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1520 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1521 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1522 | }; |
| 1523 | |
| 1524 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1525 | [secondOp-Instruction::CastOpsBegin]; |
| 1526 | switch (ElimCase) { |
| 1527 | case 0: |
| 1528 | // categorically disallowed |
| 1529 | return 0; |
| 1530 | case 1: |
| 1531 | // allowed, use first cast's opcode |
| 1532 | return firstOp; |
| 1533 | case 2: |
| 1534 | // allowed, use second cast's opcode |
| 1535 | return secondOp; |
| 1536 | case 3: |
| 1537 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1538 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1539 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1540 | return firstOp; |
| 1541 | return 0; |
| 1542 | case 4: |
| 1543 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1544 | // is floating point |
| 1545 | if (DstTy->isFloatingPoint()) |
| 1546 | return firstOp; |
| 1547 | return 0; |
| 1548 | case 5: |
| 1549 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1550 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1551 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1552 | return secondOp; |
| 1553 | return 0; |
| 1554 | case 6: |
| 1555 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1556 | // is a floating point |
| 1557 | if (SrcTy->isFloatingPoint()) |
| 1558 | return secondOp; |
| 1559 | return 0; |
| 1560 | case 7: { |
| 1561 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1562 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1563 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1564 | if (MidSize >= PtrSize) |
| 1565 | return Instruction::BitCast; |
| 1566 | return 0; |
| 1567 | } |
| 1568 | case 8: { |
| 1569 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1570 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1571 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1572 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1573 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1574 | if (SrcSize == DstSize) |
| 1575 | return Instruction::BitCast; |
| 1576 | else if (SrcSize < DstSize) |
| 1577 | return firstOp; |
| 1578 | return secondOp; |
| 1579 | } |
| 1580 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1581 | return Instruction::ZExt; |
| 1582 | case 10: |
| 1583 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1584 | // the same as the original, in which case its just a bitcast |
| 1585 | if (SrcTy == DstTy) |
| 1586 | return Instruction::BitCast; |
| 1587 | return 0; // If the types are not the same we can't eliminate it. |
| 1588 | case 11: |
| 1589 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1590 | // is a pointer to pointer cast. |
| 1591 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1592 | return secondOp; |
| 1593 | return 0; |
| 1594 | case 12: |
| 1595 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1596 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1597 | return firstOp; |
| 1598 | return 0; |
| 1599 | case 13: { |
| 1600 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1601 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1602 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1603 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1604 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1605 | return Instruction::BitCast; |
| 1606 | return 0; |
| 1607 | } |
| 1608 | case 99: |
| 1609 | // cast combination can't happen (error in input). This is for all cases |
| 1610 | // where the MidTy is not the same for the two cast instructions. |
| 1611 | assert(!"Invalid Cast Combination"); |
| 1612 | return 0; |
| 1613 | default: |
| 1614 | assert(!"Error in CastResults table!!!"); |
| 1615 | return 0; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1621 | const std::string &Name, Instruction *InsertBefore) { |
| 1622 | // Construct and return the appropriate CastInst subclass |
| 1623 | switch (op) { |
| 1624 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1625 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1626 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1627 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1628 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1629 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1630 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1631 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1632 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1633 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1634 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1635 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1636 | default: |
| 1637 | assert(!"Invalid opcode provided"); |
| 1638 | } |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1643 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1644 | // Construct and return the appropriate CastInst subclass |
| 1645 | switch (op) { |
| 1646 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1647 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1648 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1649 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1650 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1651 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1652 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1653 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1654 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1655 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1656 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1657 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1658 | default: |
| 1659 | assert(!"Invalid opcode provided"); |
| 1660 | } |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1664 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1665 | const std::string &Name, |
| 1666 | Instruction *InsertBefore) { |
| 1667 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1668 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1669 | return create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
| 1670 | } |
| 1671 | |
| 1672 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1673 | const std::string &Name, |
| 1674 | BasicBlock *InsertAtEnd) { |
| 1675 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1676 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1677 | return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
| 1678 | } |
| 1679 | |
| 1680 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1681 | const std::string &Name, |
| 1682 | Instruction *InsertBefore) { |
| 1683 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1684 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1685 | return create(Instruction::SExt, S, Ty, Name, InsertBefore); |
| 1686 | } |
| 1687 | |
| 1688 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1689 | const std::string &Name, |
| 1690 | BasicBlock *InsertAtEnd) { |
| 1691 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1692 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1693 | return create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
| 1694 | } |
| 1695 | |
| 1696 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1697 | const std::string &Name, |
| 1698 | Instruction *InsertBefore) { |
| 1699 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1700 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1701 | return create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
| 1702 | } |
| 1703 | |
| 1704 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1705 | const std::string &Name, |
| 1706 | BasicBlock *InsertAtEnd) { |
| 1707 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1708 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1709 | return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
| 1710 | } |
| 1711 | |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1712 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1713 | const std::string &Name, |
| 1714 | BasicBlock *InsertAtEnd) { |
| 1715 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1716 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1717 | "Invalid cast"); |
| 1718 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1719 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1720 | return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 1721 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1722 | } |
| 1723 | |
| 1724 | /// @brief Create a BitCast or a PtrToInt cast instruction |
| 1725 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1726 | const std::string &Name, |
| 1727 | Instruction *InsertBefore) { |
| 1728 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1729 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1730 | "Invalid cast"); |
| 1731 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1732 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1733 | return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 1734 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1735 | } |
| 1736 | |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1737 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1738 | bool isSigned, const std::string &Name, |
| 1739 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1740 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1741 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1742 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1743 | Instruction::CastOps opcode = |
| 1744 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1745 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1746 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1747 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1748 | } |
| 1749 | |
| 1750 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1751 | bool isSigned, const std::string &Name, |
| 1752 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1753 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1754 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1755 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1756 | Instruction::CastOps opcode = |
| 1757 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1758 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1759 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1760 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1761 | } |
| 1762 | |
| 1763 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1764 | const std::string &Name, |
| 1765 | Instruction *InsertBefore) { |
| 1766 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1767 | "Invalid cast"); |
| 1768 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1769 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1770 | Instruction::CastOps opcode = |
| 1771 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1772 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1773 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1774 | } |
| 1775 | |
| 1776 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1777 | const std::string &Name, |
| 1778 | BasicBlock *InsertAtEnd) { |
| 1779 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1780 | "Invalid cast"); |
| 1781 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1782 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1783 | Instruction::CastOps opcode = |
| 1784 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1785 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1786 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1787 | } |
| 1788 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1789 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 1790 | // 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] | 1791 | // logic in the castIsValid function below. This axiom should hold: |
| 1792 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 1793 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1794 | // casting opcode for the arguments passed to it. |
| 1795 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1796 | CastInst::getCastOpcode( |
| 1797 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1798 | // Get the bit sizes, we'll need these |
| 1799 | const Type *SrcTy = Src->getType(); |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1800 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
| 1801 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1804 | if (DestTy->isInteger()) { // Casting to integral |
| 1805 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1806 | if (DestBits < SrcBits) |
| 1807 | return Trunc; // int -> smaller int |
| 1808 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1809 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1810 | return SExt; // signed -> SEXT |
| 1811 | else |
| 1812 | return ZExt; // unsigned -> ZEXT |
| 1813 | } else { |
| 1814 | return BitCast; // Same size, No-op cast |
| 1815 | } |
| 1816 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1817 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1818 | return FPToSI; // FP -> sint |
| 1819 | else |
| 1820 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1821 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1822 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1823 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1824 | return BitCast; // Same size, no-op cast |
| 1825 | } else { |
| 1826 | assert(isa<PointerType>(SrcTy) && |
| 1827 | "Casting from a value that is not first-class type"); |
| 1828 | return PtrToInt; // ptr -> int |
| 1829 | } |
| 1830 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1831 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1832 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1833 | return SIToFP; // sint -> FP |
| 1834 | else |
| 1835 | return UIToFP; // uint -> FP |
| 1836 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1837 | if (DestBits < SrcBits) { |
| 1838 | return FPTrunc; // FP -> smaller FP |
| 1839 | } else if (DestBits > SrcBits) { |
| 1840 | return FPExt; // FP -> larger FP |
| 1841 | } else { |
| 1842 | return BitCast; // same size, no-op cast |
| 1843 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1844 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1845 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1846 | "Casting vector to floating point of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1847 | return BitCast; // same size, no-op cast |
| 1848 | } else { |
| 1849 | assert(0 && "Casting pointer or non-first class to float"); |
| 1850 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1851 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 1852 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1853 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1854 | "Casting vector to vector of different widths"); |
| 1855 | return BitCast; // vector -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1856 | } else if (DestPTy->getBitWidth() == SrcBits) { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1857 | return BitCast; // float/int -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1858 | } else { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1859 | assert(!"Illegal cast to vector (wrong type or size)"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1860 | } |
| 1861 | } else if (isa<PointerType>(DestTy)) { |
| 1862 | if (isa<PointerType>(SrcTy)) { |
| 1863 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1864 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1865 | return IntToPtr; // int -> ptr |
| 1866 | } else { |
| 1867 | assert(!"Casting pointer to other than pointer or int"); |
| 1868 | } |
| 1869 | } else { |
| 1870 | assert(!"Casting to type that is not first-class"); |
| 1871 | } |
| 1872 | |
| 1873 | // If we fall through to here we probably hit an assertion cast above |
| 1874 | // and assertions are not turned on. Anything we return is an error, so |
| 1875 | // BitCast is as good a choice as any. |
| 1876 | return BitCast; |
| 1877 | } |
| 1878 | |
| 1879 | //===----------------------------------------------------------------------===// |
| 1880 | // CastInst SubClass Constructors |
| 1881 | //===----------------------------------------------------------------------===// |
| 1882 | |
| 1883 | /// Check that the construction parameters for a CastInst are correct. This |
| 1884 | /// could be broken out into the separate constructors but it is useful to have |
| 1885 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 1886 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1887 | bool |
| 1888 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Check for type sanity on the arguments |
| 1891 | const Type *SrcTy = S->getType(); |
| 1892 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 1893 | return false; |
| 1894 | |
| 1895 | // Get the size of the types in bits, we'll need this later |
| 1896 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1897 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1898 | |
| 1899 | // Switch on the opcode provided |
| 1900 | switch (op) { |
| 1901 | default: return false; // This is an input error |
| 1902 | case Instruction::Trunc: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1903 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1904 | case Instruction::ZExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1905 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1906 | case Instruction::SExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1907 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1908 | case Instruction::FPTrunc: |
| 1909 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1910 | SrcBitSize > DstBitSize; |
| 1911 | case Instruction::FPExt: |
| 1912 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1913 | SrcBitSize < DstBitSize; |
| 1914 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1915 | case Instruction::SIToFP: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1916 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 1917 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 1918 | return SVTy->getElementType()->isInteger() && |
| 1919 | DVTy->getElementType()->isFloatingPoint() && |
| 1920 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 1921 | } |
| 1922 | } |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1923 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1924 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1925 | case Instruction::FPToSI: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1926 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 1927 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 1928 | return SVTy->getElementType()->isFloatingPoint() && |
| 1929 | DVTy->getElementType()->isInteger() && |
| 1930 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 1931 | } |
| 1932 | } |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1933 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1934 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1935 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1936 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1937 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1938 | case Instruction::BitCast: |
| 1939 | // BitCast implies a no-op cast of type only. No bits change. |
| 1940 | // However, you can't cast pointers to anything but pointers. |
| 1941 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 1942 | return false; |
| 1943 | |
| 1944 | // Now we know we're not dealing with a pointer/non-poiner mismatch. In all |
| 1945 | // these cases, the cast is okay if the source and destination bit widths |
| 1946 | // are identical. |
| 1947 | return SrcBitSize == DstBitSize; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | TruncInst::TruncInst( |
| 1952 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1953 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1954 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | TruncInst::TruncInst( |
| 1958 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1959 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1960 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | ZExtInst::ZExtInst( |
| 1964 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1965 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1966 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | ZExtInst::ZExtInst( |
| 1970 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1971 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1972 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1973 | } |
| 1974 | SExtInst::SExtInst( |
| 1975 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1976 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1977 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 1980 | SExtInst::SExtInst( |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1981 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1982 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1983 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | FPTruncInst::FPTruncInst( |
| 1987 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1988 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1989 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | FPTruncInst::FPTruncInst( |
| 1993 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1994 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1995 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | FPExtInst::FPExtInst( |
| 1999 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2000 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2001 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | FPExtInst::FPExtInst( |
| 2005 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2006 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2007 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | UIToFPInst::UIToFPInst( |
| 2011 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2012 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2013 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | UIToFPInst::UIToFPInst( |
| 2017 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2018 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2019 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | SIToFPInst::SIToFPInst( |
| 2023 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2024 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2025 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | SIToFPInst::SIToFPInst( |
| 2029 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2030 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2031 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | FPToUIInst::FPToUIInst( |
| 2035 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2036 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2037 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | FPToUIInst::FPToUIInst( |
| 2041 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2042 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2043 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | FPToSIInst::FPToSIInst( |
| 2047 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2048 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2049 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | FPToSIInst::FPToSIInst( |
| 2053 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2054 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2055 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | PtrToIntInst::PtrToIntInst( |
| 2059 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2060 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2061 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | PtrToIntInst::PtrToIntInst( |
| 2065 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2066 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2067 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | IntToPtrInst::IntToPtrInst( |
| 2071 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2072 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2073 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | IntToPtrInst::IntToPtrInst( |
| 2077 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2078 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2079 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | BitCastInst::BitCastInst( |
| 2083 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2084 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2085 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | BitCastInst::BitCastInst( |
| 2089 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2090 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2091 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2092 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2093 | |
| 2094 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2095 | // CmpInst Classes |
| 2096 | //===----------------------------------------------------------------------===// |
| 2097 | |
| 2098 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 2099 | const std::string &Name, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2100 | : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2101 | Ops[0].init(LHS, this); |
| 2102 | Ops[1].init(RHS, this); |
| 2103 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2104 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2105 | if (op == Instruction::ICmp) { |
| 2106 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 2107 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 2108 | "Invalid ICmp predicate value"); |
| 2109 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2110 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2111 | assert(Op0Ty == Op1Ty && |
| 2112 | "Both operands to ICmp instruction are not of the same type!"); |
| 2113 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2114 | assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2115 | "Invalid operand types for ICmp instruction"); |
| 2116 | return; |
| 2117 | } |
| 2118 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 2119 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 2120 | "Invalid FCmp predicate value"); |
| 2121 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2122 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2123 | assert(Op0Ty == Op1Ty && |
| 2124 | "Both operands to FCmp instruction are not of the same type!"); |
| 2125 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2126 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2127 | "Invalid operand types for FCmp instruction"); |
| 2128 | } |
| 2129 | |
| 2130 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 2131 | const std::string &Name, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2132 | : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2133 | Ops[0].init(LHS, this); |
| 2134 | Ops[1].init(RHS, this); |
| 2135 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2136 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2137 | if (op == Instruction::ICmp) { |
| 2138 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 2139 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 2140 | "Invalid ICmp predicate value"); |
| 2141 | |
| 2142 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2143 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2144 | assert(Op0Ty == Op1Ty && |
| 2145 | "Both operands to ICmp instruction are not of the same type!"); |
| 2146 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2147 | assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2148 | "Invalid operand types for ICmp instruction"); |
| 2149 | return; |
| 2150 | } |
| 2151 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 2152 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 2153 | "Invalid FCmp predicate value"); |
| 2154 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2155 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2156 | assert(Op0Ty == Op1Ty && |
| 2157 | "Both operands to FCmp instruction are not of the same type!"); |
| 2158 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2159 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2160 | "Invalid operand types for FCmp instruction"); |
| 2161 | } |
| 2162 | |
| 2163 | CmpInst * |
| 2164 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2165 | const std::string &Name, Instruction *InsertBefore) { |
| 2166 | if (Op == Instruction::ICmp) { |
| 2167 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2168 | InsertBefore); |
| 2169 | } |
| 2170 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2171 | InsertBefore); |
| 2172 | } |
| 2173 | |
| 2174 | CmpInst * |
| 2175 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2176 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 2177 | if (Op == Instruction::ICmp) { |
| 2178 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2179 | InsertAtEnd); |
| 2180 | } |
| 2181 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2182 | InsertAtEnd); |
| 2183 | } |
| 2184 | |
| 2185 | void CmpInst::swapOperands() { |
| 2186 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2187 | IC->swapOperands(); |
| 2188 | else |
| 2189 | cast<FCmpInst>(this)->swapOperands(); |
| 2190 | } |
| 2191 | |
| 2192 | bool CmpInst::isCommutative() { |
| 2193 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2194 | return IC->isCommutative(); |
| 2195 | return cast<FCmpInst>(this)->isCommutative(); |
| 2196 | } |
| 2197 | |
| 2198 | bool CmpInst::isEquality() { |
| 2199 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2200 | return IC->isEquality(); |
| 2201 | return cast<FCmpInst>(this)->isEquality(); |
| 2202 | } |
| 2203 | |
| 2204 | |
| 2205 | ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { |
| 2206 | switch (pred) { |
| 2207 | default: |
| 2208 | assert(!"Unknown icmp predicate!"); |
| 2209 | case ICMP_EQ: return ICMP_NE; |
| 2210 | case ICMP_NE: return ICMP_EQ; |
| 2211 | case ICMP_UGT: return ICMP_ULE; |
| 2212 | case ICMP_ULT: return ICMP_UGE; |
| 2213 | case ICMP_UGE: return ICMP_ULT; |
| 2214 | case ICMP_ULE: return ICMP_UGT; |
| 2215 | case ICMP_SGT: return ICMP_SLE; |
| 2216 | case ICMP_SLT: return ICMP_SGE; |
| 2217 | case ICMP_SGE: return ICMP_SLT; |
| 2218 | case ICMP_SLE: return ICMP_SGT; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { |
| 2223 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2224 | default: assert(! "Unknown icmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2225 | case ICMP_EQ: case ICMP_NE: |
| 2226 | return pred; |
| 2227 | case ICMP_SGT: return ICMP_SLT; |
| 2228 | case ICMP_SLT: return ICMP_SGT; |
| 2229 | case ICMP_SGE: return ICMP_SLE; |
| 2230 | case ICMP_SLE: return ICMP_SGE; |
| 2231 | case ICMP_UGT: return ICMP_ULT; |
| 2232 | case ICMP_ULT: return ICMP_UGT; |
| 2233 | case ICMP_UGE: return ICMP_ULE; |
| 2234 | case ICMP_ULE: return ICMP_UGE; |
| 2235 | } |
| 2236 | } |
| 2237 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2238 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2239 | switch (pred) { |
| 2240 | default: assert(! "Unknown icmp predicate!"); |
| 2241 | case ICMP_EQ: case ICMP_NE: |
| 2242 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2243 | return pred; |
| 2244 | case ICMP_UGT: return ICMP_SGT; |
| 2245 | case ICMP_ULT: return ICMP_SLT; |
| 2246 | case ICMP_UGE: return ICMP_SGE; |
| 2247 | case ICMP_ULE: return ICMP_SLE; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2252 | switch (pred) { |
| 2253 | default: assert(! "Unknown icmp predicate!"); |
| 2254 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2255 | return true; |
| 2256 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2257 | case ICMP_UGE: case ICMP_ULE: |
| 2258 | return false; |
| 2259 | } |
| 2260 | } |
| 2261 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2262 | /// Initialize a set of values that all satisfy the condition with C. |
| 2263 | /// |
| 2264 | ConstantRange |
| 2265 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2266 | APInt Lower(C); |
| 2267 | APInt Upper(C); |
| 2268 | uint32_t BitWidth = C.getBitWidth(); |
| 2269 | switch (pred) { |
| 2270 | default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!"); |
| 2271 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2272 | case ICmpInst::ICMP_NE: Lower++; break; |
| 2273 | case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break; |
| 2274 | case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break; |
| 2275 | case ICmpInst::ICMP_UGT: |
| 2276 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2277 | break; |
| 2278 | case ICmpInst::ICMP_SGT: |
| 2279 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2280 | break; |
| 2281 | case ICmpInst::ICMP_ULE: |
| 2282 | Lower = APInt::getMinValue(BitWidth); Upper++; |
| 2283 | break; |
| 2284 | case ICmpInst::ICMP_SLE: |
| 2285 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
| 2286 | break; |
| 2287 | case ICmpInst::ICMP_UGE: |
| 2288 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2289 | break; |
| 2290 | case ICmpInst::ICMP_SGE: |
| 2291 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2292 | break; |
| 2293 | } |
| 2294 | return ConstantRange(Lower, Upper); |
| 2295 | } |
| 2296 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2297 | FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { |
| 2298 | switch (pred) { |
| 2299 | default: |
| 2300 | assert(!"Unknown icmp predicate!"); |
| 2301 | case FCMP_OEQ: return FCMP_UNE; |
| 2302 | case FCMP_ONE: return FCMP_UEQ; |
| 2303 | case FCMP_OGT: return FCMP_ULE; |
| 2304 | case FCMP_OLT: return FCMP_UGE; |
| 2305 | case FCMP_OGE: return FCMP_ULT; |
| 2306 | case FCMP_OLE: return FCMP_UGT; |
| 2307 | case FCMP_UEQ: return FCMP_ONE; |
| 2308 | case FCMP_UNE: return FCMP_OEQ; |
| 2309 | case FCMP_UGT: return FCMP_OLE; |
| 2310 | case FCMP_ULT: return FCMP_OGE; |
| 2311 | case FCMP_UGE: return FCMP_OLT; |
| 2312 | case FCMP_ULE: return FCMP_OGT; |
| 2313 | case FCMP_ORD: return FCMP_UNO; |
| 2314 | case FCMP_UNO: return FCMP_ORD; |
| 2315 | case FCMP_TRUE: return FCMP_FALSE; |
| 2316 | case FCMP_FALSE: return FCMP_TRUE; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { |
| 2321 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2322 | default: assert(!"Unknown fcmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2323 | case FCMP_FALSE: case FCMP_TRUE: |
| 2324 | case FCMP_OEQ: case FCMP_ONE: |
| 2325 | case FCMP_UEQ: case FCMP_UNE: |
| 2326 | case FCMP_ORD: case FCMP_UNO: |
| 2327 | return pred; |
| 2328 | case FCMP_OGT: return FCMP_OLT; |
| 2329 | case FCMP_OLT: return FCMP_OGT; |
| 2330 | case FCMP_OGE: return FCMP_OLE; |
| 2331 | case FCMP_OLE: return FCMP_OGE; |
| 2332 | case FCMP_UGT: return FCMP_ULT; |
| 2333 | case FCMP_ULT: return FCMP_UGT; |
| 2334 | case FCMP_UGE: return FCMP_ULE; |
| 2335 | case FCMP_ULE: return FCMP_UGE; |
| 2336 | } |
| 2337 | } |
| 2338 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2339 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2340 | switch (predicate) { |
| 2341 | default: return false; |
| 2342 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2343 | case ICmpInst::ICMP_UGE: return true; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2348 | switch (predicate) { |
| 2349 | default: return false; |
| 2350 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2351 | case ICmpInst::ICMP_SGE: return true; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2356 | switch (predicate) { |
| 2357 | default: return false; |
| 2358 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2359 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2360 | case FCmpInst::FCMP_ORD: return true; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2365 | switch (predicate) { |
| 2366 | default: return false; |
| 2367 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2368 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2369 | case FCmpInst::FCMP_UNO: return true; |
| 2370 | } |
| 2371 | } |
| 2372 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2373 | //===----------------------------------------------------------------------===// |
| 2374 | // SwitchInst Implementation |
| 2375 | //===----------------------------------------------------------------------===// |
| 2376 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2377 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2378 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2379 | ReservedSpace = 2+NumCases*2; |
| 2380 | NumOperands = 2; |
| 2381 | OperandList = new Use[ReservedSpace]; |
| 2382 | |
| 2383 | OperandList[0].init(Value, this); |
| 2384 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2387 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2388 | /// switch on and a default destination. The number of additional cases can |
| 2389 | /// be specified here to make memory allocation more efficient. This |
| 2390 | /// constructor can also autoinsert before another instruction. |
| 2391 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2392 | Instruction *InsertBefore) |
| 2393 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) { |
| 2394 | init(Value, Default, NumCases); |
| 2395 | } |
| 2396 | |
| 2397 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2398 | /// switch on and a default destination. The number of additional cases can |
| 2399 | /// be specified here to make memory allocation more efficient. This |
| 2400 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 2401 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2402 | BasicBlock *InsertAtEnd) |
| 2403 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) { |
| 2404 | init(Value, Default, NumCases); |
| 2405 | } |
| 2406 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2407 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2408 | : TerminatorInst(Type::VoidTy, Instruction::Switch, |
| 2409 | new Use[SI.getNumOperands()], SI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2410 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2411 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 2412 | OL[i].init(InOL[i], this); |
| 2413 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2417 | SwitchInst::~SwitchInst() { |
| 2418 | delete [] OperandList; |
| 2419 | } |
| 2420 | |
| 2421 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2422 | /// addCase - Add an entry to the switch instruction... |
| 2423 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2424 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2425 | unsigned OpNo = NumOperands; |
| 2426 | if (OpNo+2 > ReservedSpace) |
| 2427 | resizeOperands(0); // Get more space! |
| 2428 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2429 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2430 | NumOperands = OpNo+2; |
| 2431 | OperandList[OpNo].init(OnVal, this); |
| 2432 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /// removeCase - This method removes the specified successor from the switch |
| 2436 | /// instruction. Note that this cannot be used to remove the default |
| 2437 | /// destination (successor #0). |
| 2438 | /// |
| 2439 | void SwitchInst::removeCase(unsigned idx) { |
| 2440 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2441 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2442 | |
| 2443 | unsigned NumOps = getNumOperands(); |
| 2444 | Use *OL = OperandList; |
| 2445 | |
| 2446 | // Move everything after this operand down. |
| 2447 | // |
| 2448 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2449 | // client might not expect this to happen. The code as it is thrashes the |
| 2450 | // use/def lists, which is kinda lame. |
| 2451 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2452 | OL[i-2] = OL[i]; |
| 2453 | OL[i-2+1] = OL[i+1]; |
| 2454 | } |
| 2455 | |
| 2456 | // Nuke the last value. |
| 2457 | OL[NumOps-2].set(0); |
| 2458 | OL[NumOps-2+1].set(0); |
| 2459 | NumOperands = NumOps-2; |
| 2460 | } |
| 2461 | |
| 2462 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2463 | /// list according to the following behavior: |
| 2464 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 2465 | /// of operation. This grows the number of ops by 1.5 times. |
| 2466 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2467 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2468 | /// |
| 2469 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 2470 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2471 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2472 | } else if (NumOps*2 > NumOperands) { |
| 2473 | // No resize needed. |
| 2474 | if (ReservedSpace >= NumOps) return; |
| 2475 | } else if (NumOps == NumOperands) { |
| 2476 | if (ReservedSpace == NumOps) return; |
| 2477 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2478 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | ReservedSpace = NumOps; |
| 2482 | Use *NewOps = new Use[NumOps]; |
| 2483 | Use *OldOps = OperandList; |
| 2484 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2485 | NewOps[i].init(OldOps[i], this); |
| 2486 | OldOps[i].set(0); |
| 2487 | } |
| 2488 | delete [] OldOps; |
| 2489 | OperandList = NewOps; |
| 2490 | } |
| 2491 | |
| 2492 | |
| 2493 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2494 | return getSuccessor(idx); |
| 2495 | } |
| 2496 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2497 | return getNumSuccessors(); |
| 2498 | } |
| 2499 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2500 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2501 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2502 | |
| 2503 | |
| 2504 | // Define these methods here so vtables don't get emitted into every translation |
| 2505 | // unit that uses these classes. |
| 2506 | |
| 2507 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 2508 | return new GetElementPtrInst(*this); |
| 2509 | } |
| 2510 | |
| 2511 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2512 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Chris Lattner | 0b490b0 | 2007-08-24 20:48:18 +0000 | [diff] [blame] | 2515 | FCmpInst* FCmpInst::clone() const { |
| 2516 | return new FCmpInst(getPredicate(), Ops[0], Ops[1]); |
| 2517 | } |
| 2518 | ICmpInst* ICmpInst::clone() const { |
| 2519 | return new ICmpInst(getPredicate(), Ops[0], Ops[1]); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2522 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2523 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2524 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2525 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2526 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2527 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2528 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2529 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2530 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2531 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2532 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2533 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2534 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2535 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2536 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2537 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2538 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
| 2539 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2540 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
| 2541 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2542 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2543 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2544 | return new ExtractElementInst(*this); |
| 2545 | } |
| 2546 | InsertElementInst *InsertElementInst::clone() const { |
| 2547 | return new InsertElementInst(*this); |
| 2548 | } |
| 2549 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2550 | return new ShuffleVectorInst(*this); |
| 2551 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2552 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 2553 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 2554 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 2555 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 2556 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 2557 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2558 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |