Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef sw_Nucleus_hpp |
| 16 | #define sw_Nucleus_hpp |
| 17 | |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 18 | #include <cstdarg> |
| 19 | #include <cstdint> |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 20 | #include <vector> |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 22 | namespace sw |
| 23 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 24 | class Type; |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 25 | class Value; |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 26 | class BasicBlock; |
Nicolas Capens | daa5d91 | 2016-09-28 16:56:36 -0400 | [diff] [blame] | 27 | class Routine; |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 28 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 29 | enum Optimization |
| 30 | { |
| 31 | Disabled = 0, |
| 32 | InstructionCombining = 1, |
| 33 | CFGSimplification = 2, |
| 34 | LICM = 3, |
| 35 | AggressiveDCE = 4, |
| 36 | GVN = 5, |
| 37 | Reassociate = 6, |
| 38 | DeadStoreElimination = 7, |
| 39 | SCCP = 8, |
| 40 | ScalarReplAggregates = 9, |
| 41 | |
| 42 | OptimizationCount |
| 43 | }; |
| 44 | |
| 45 | extern Optimization optimization[10]; |
| 46 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 47 | class Nucleus |
| 48 | { |
| 49 | public: |
| 50 | Nucleus(); |
| 51 | |
| 52 | virtual ~Nucleus(); |
| 53 | |
| 54 | Routine *acquireRoutine(const wchar_t *name, bool runOptimizations = true); |
| 55 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 56 | static Value *allocateStackVariable(Type *type, int arraySize = 0); |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 57 | static BasicBlock *createBasicBlock(); |
| 58 | static BasicBlock *getInsertBlock(); |
| 59 | static void setInsertBlock(BasicBlock *basicBlock); |
| 60 | static BasicBlock *getPredecessor(BasicBlock *basicBlock); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 61 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 62 | static void createFunction(Type *ReturnType, std::vector<Type*> &Params); |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 63 | static Value *getArgument(unsigned int index); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 64 | |
| 65 | // Terminators |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 66 | static void createRetVoid(); |
| 67 | static void createRet(Value *V); |
| 68 | static void createBr(BasicBlock *dest); |
| 69 | static void createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 70 | |
| 71 | // Binary operators |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 72 | static Value *createAdd(Value *lhs, Value *rhs); |
| 73 | static Value *createSub(Value *lhs, Value *rhs); |
| 74 | static Value *createMul(Value *lhs, Value *rhs); |
| 75 | static Value *createUDiv(Value *lhs, Value *rhs); |
| 76 | static Value *createSDiv(Value *lhs, Value *rhs); |
| 77 | static Value *createFAdd(Value *lhs, Value *rhs); |
| 78 | static Value *createFSub(Value *lhs, Value *rhs); |
| 79 | static Value *createFMul(Value *lhs, Value *rhs); |
| 80 | static Value *createFDiv(Value *lhs, Value *rhs); |
| 81 | static Value *createURem(Value *lhs, Value *rhs); |
| 82 | static Value *createSRem(Value *lhs, Value *rhs); |
| 83 | static Value *createFRem(Value *lhs, Value *rhs); |
| 84 | static Value *createShl(Value *lhs, Value *rhs); |
| 85 | static Value *createLShr(Value *lhs, Value *rhs); |
| 86 | static Value *createAShr(Value *lhs, Value *rhs); |
| 87 | static Value *createAnd(Value *lhs, Value *rhs); |
| 88 | static Value *createOr(Value *lhs, Value *rhs); |
| 89 | static Value *createXor(Value *lhs, Value *rhs); |
Nicolas Capens | b955d5b | 2016-09-28 22:36:28 -0400 | [diff] [blame] | 90 | |
| 91 | // Unary operators |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 92 | static Value *createNeg(Value *V); |
| 93 | static Value *createFNeg(Value *V); |
| 94 | static Value *createNot(Value *V); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 95 | |
| 96 | // Memory instructions |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 97 | static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); |
Nicolas Capens | 6d73871 | 2016-09-30 04:15:22 -0400 | [diff] [blame] | 98 | static Value *createStore(Value *value, Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); |
Nicolas Capens | 6d73871 | 2016-09-30 04:15:22 -0400 | [diff] [blame] | 99 | static Value *createGEP(Value *ptr, Type *type, Value *index); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 100 | |
| 101 | // Atomic instructions |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 102 | static Value *createAtomicAdd(Value *ptr, Value *value); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 103 | |
| 104 | // Cast/Conversion Operators |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 105 | static Value *createTrunc(Value *V, Type *destType); |
| 106 | static Value *createZExt(Value *V, Type *destType); |
| 107 | static Value *createSExt(Value *V, Type *destType); |
| 108 | static Value *createFPToSI(Value *V, Type *destType); |
| 109 | static Value *createUIToFP(Value *V, Type *destType); |
| 110 | static Value *createSIToFP(Value *V, Type *destType); |
| 111 | static Value *createFPTrunc(Value *V, Type *destType); |
| 112 | static Value *createFPExt(Value *V, Type *destType); |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 113 | static Value *createBitCast(Value *V, Type *destType); |
| 114 | static Value *createIntCast(Value *V, Type *destType, bool isSigned); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 115 | |
| 116 | // Compare instructions |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 117 | static Value *createICmpEQ(Value *lhs, Value *rhs); |
| 118 | static Value *createICmpNE(Value *lhs, Value *rhs); |
| 119 | static Value *createICmpUGT(Value *lhs, Value *rhs); |
| 120 | static Value *createICmpUGE(Value *lhs, Value *rhs); |
| 121 | static Value *createICmpULT(Value *lhs, Value *rhs); |
| 122 | static Value *createICmpULE(Value *lhs, Value *rhs); |
| 123 | static Value *createICmpSGT(Value *lhs, Value *rhs); |
| 124 | static Value *createICmpSGE(Value *lhs, Value *rhs); |
| 125 | static Value *createICmpSLT(Value *lhs, Value *rhs); |
| 126 | static Value *createICmpSLE(Value *lhs, Value *rhs); |
| 127 | static Value *createFCmpOEQ(Value *lhs, Value *rhs); |
| 128 | static Value *createFCmpOGT(Value *lhs, Value *rhs); |
| 129 | static Value *createFCmpOGE(Value *lhs, Value *rhs); |
| 130 | static Value *createFCmpOLT(Value *lhs, Value *rhs); |
| 131 | static Value *createFCmpOLE(Value *lhs, Value *rhs); |
| 132 | static Value *createFCmpONE(Value *lhs, Value *rhs); |
| 133 | static Value *createFCmpORD(Value *lhs, Value *rhs); |
| 134 | static Value *createFCmpUNO(Value *lhs, Value *rhs); |
| 135 | static Value *createFCmpUEQ(Value *lhs, Value *rhs); |
| 136 | static Value *createFCmpUGT(Value *lhs, Value *rhs); |
| 137 | static Value *createFCmpUGE(Value *lhs, Value *rhs); |
| 138 | static Value *createFCmpULT(Value *lhs, Value *rhs); |
| 139 | static Value *createFCmpULE(Value *lhs, Value *rhs); |
| 140 | static Value *createFCmpUNE(Value *lhs, Value *rhs); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 141 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 142 | // Vector instructions |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 143 | static Value *createExtractElement(Value *vector, Type *type, int index); |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 144 | static Value *createInsertElement(Value *vector, Value *element, int index); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 145 | static Value *createShuffleVector(Value *V1, Value *V2, const int *select); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 146 | |
| 147 | // Other instructions |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 148 | static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse); |
| 149 | static Value *createSwitch(Value *V, BasicBlock *Dest, unsigned NumCases); |
| 150 | static void addSwitchCase(Value *Switch, int Case, BasicBlock *Branch); |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 151 | static void createUnreachable(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 152 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 153 | // Constant values |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 154 | static Value *createNullValue(Type *type); |
| 155 | static Value *createConstantLong(int64_t i); |
| 156 | static Value *createConstantInt(int i); |
| 157 | static Value *createConstantInt(unsigned int i); |
| 158 | static Value *createConstantBool(bool b); |
| 159 | static Value *createConstantByte(signed char i); |
| 160 | static Value *createConstantByte(unsigned char i); |
| 161 | static Value *createConstantShort(short i); |
| 162 | static Value *createConstantShort(unsigned short i); |
| 163 | static Value *createConstantFloat(float x); |
| 164 | static Value *createNullPointer(Type *type); |
| 165 | static Value *createConstantVector(const int64_t *constants, Type *type); |
| 166 | static Value *createConstantVector(const double *constants, Type *type); |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame^] | 167 | static Value *createConstantPointer(const void *external, Type *type, unsigned int align = 0); |
Nicolas Capens | 0e33ae3 | 2016-09-23 17:37:27 -0400 | [diff] [blame] | 168 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 169 | static Type *getPointerType(Type *elementType); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 170 | |
| 171 | private: |
| 172 | void optimize(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 173 | }; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | #endif // sw_Nucleus_hpp |