|
| 1 | +/* |
| 2 | + * Copyright 2017 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Lowers unaligned loads and stores into aligned loads and stores |
| 19 | +// that are smaller. This leaves only aligned operations. |
| 20 | +// |
| 21 | + |
| 22 | +#include "ir/bits.h" |
| 23 | +#include "pass.h" |
| 24 | +#include "wasm-builder.h" |
| 25 | +#include "wasm.h" |
| 26 | + |
| 27 | +namespace wasm { |
| 28 | + |
| 29 | +struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { |
| 30 | + void visitLoad(Load* curr) { |
| 31 | + if (curr->align == 0 || curr->align == curr->bytes) { |
| 32 | + return; |
| 33 | + } |
| 34 | + Builder builder(*getModule()); |
| 35 | + if (curr->type == unreachable) { |
| 36 | + replaceCurrent(curr->ptr); |
| 37 | + return; |
| 38 | + } |
| 39 | + assert(curr->type == i32); // TODO: i64, f32, f64 |
| 40 | + auto temp = builder.addVar(getFunction(), i32); |
| 41 | + Expression* ret; |
| 42 | + if (curr->bytes == 2) { |
| 43 | + ret = builder.makeBinary( |
| 44 | + OrInt32, |
| 45 | + builder.makeLoad( |
| 46 | + 1, false, curr->offset, 1, builder.makeGetLocal(temp, i32), i32), |
| 47 | + builder.makeBinary(ShlInt32, |
| 48 | + builder.makeLoad(1, |
| 49 | + false, |
| 50 | + curr->offset + 1, |
| 51 | + 1, |
| 52 | + builder.makeGetLocal(temp, i32), |
| 53 | + i32), |
| 54 | + builder.makeConst(Literal(int32_t(8))))); |
| 55 | + if (curr->signed_) { |
| 56 | + ret = Bits::makeSignExt(ret, 2, *getModule()); |
| 57 | + } |
| 58 | + } else if (curr->bytes == 4) { |
| 59 | + if (curr->align == 1) { |
| 60 | + ret = builder.makeBinary( |
| 61 | + OrInt32, |
| 62 | + builder.makeBinary( |
| 63 | + OrInt32, |
| 64 | + builder.makeLoad( |
| 65 | + 1, false, curr->offset, 1, builder.makeGetLocal(temp, i32), i32), |
| 66 | + builder.makeBinary(ShlInt32, |
| 67 | + builder.makeLoad(1, |
| 68 | + false, |
| 69 | + curr->offset + 1, |
| 70 | + 1, |
| 71 | + builder.makeGetLocal(temp, i32), |
| 72 | + i32), |
| 73 | + builder.makeConst(Literal(int32_t(8))))), |
| 74 | + builder.makeBinary( |
| 75 | + OrInt32, |
| 76 | + builder.makeBinary(ShlInt32, |
| 77 | + builder.makeLoad(1, |
| 78 | + false, |
| 79 | + curr->offset + 2, |
| 80 | + 1, |
| 81 | + builder.makeGetLocal(temp, i32), |
| 82 | + i32), |
| 83 | + builder.makeConst(Literal(int32_t(16)))), |
| 84 | + builder.makeBinary(ShlInt32, |
| 85 | + builder.makeLoad(1, |
| 86 | + false, |
| 87 | + curr->offset + 3, |
| 88 | + 1, |
| 89 | + builder.makeGetLocal(temp, i32), |
| 90 | + i32), |
| 91 | + builder.makeConst(Literal(int32_t(24)))))); |
| 92 | + } else if (curr->align == 2) { |
| 93 | + ret = builder.makeBinary( |
| 94 | + OrInt32, |
| 95 | + builder.makeLoad( |
| 96 | + 2, false, curr->offset, 2, builder.makeGetLocal(temp, i32), i32), |
| 97 | + builder.makeBinary(ShlInt32, |
| 98 | + builder.makeLoad(2, |
| 99 | + false, |
| 100 | + curr->offset + 2, |
| 101 | + 2, |
| 102 | + builder.makeGetLocal(temp, i32), |
| 103 | + i32), |
| 104 | + builder.makeConst(Literal(int32_t(16))))); |
| 105 | + } else { |
| 106 | + WASM_UNREACHABLE(); |
| 107 | + } |
| 108 | + } else { |
| 109 | + WASM_UNREACHABLE(); |
| 110 | + } |
| 111 | + replaceCurrent( |
| 112 | + builder.makeBlock({builder.makeSetLocal(temp, curr->ptr), ret})); |
| 113 | + } |
| 114 | + |
| 115 | + void visitStore(Store* curr) { |
| 116 | + if (curr->align == 0 || curr->align == curr->bytes) { |
| 117 | + return; |
| 118 | + } |
| 119 | + Builder builder(*getModule()); |
| 120 | + if (curr->type == unreachable) { |
| 121 | + replaceCurrent(builder.makeBlock( |
| 122 | + {builder.makeDrop(curr->ptr), builder.makeDrop(curr->value)})); |
| 123 | + return; |
| 124 | + } |
| 125 | + assert(curr->value->type == i32); // TODO: i64, f32, f64 |
| 126 | + auto tempPtr = builder.addVar(getFunction(), i32); |
| 127 | + auto tempValue = builder.addVar(getFunction(), i32); |
| 128 | + auto* block = |
| 129 | + builder.makeBlock({builder.makeSetLocal(tempPtr, curr->ptr), |
| 130 | + builder.makeSetLocal(tempValue, curr->value)}); |
| 131 | + if (curr->bytes == 2) { |
| 132 | + block->list.push_back( |
| 133 | + builder.makeStore(1, |
| 134 | + curr->offset, |
| 135 | + 1, |
| 136 | + builder.makeGetLocal(tempPtr, i32), |
| 137 | + builder.makeGetLocal(tempValue, i32), |
| 138 | + i32)); |
| 139 | + block->list.push_back(builder.makeStore( |
| 140 | + 1, |
| 141 | + curr->offset + 1, |
| 142 | + 1, |
| 143 | + builder.makeGetLocal(tempPtr, i32), |
| 144 | + builder.makeBinary(ShrUInt32, |
| 145 | + builder.makeGetLocal(tempValue, i32), |
| 146 | + builder.makeConst(Literal(int32_t(8)))), |
| 147 | + i32)); |
| 148 | + } else if (curr->bytes == 4) { |
| 149 | + if (curr->align == 1) { |
| 150 | + block->list.push_back( |
| 151 | + builder.makeStore(1, |
| 152 | + curr->offset, |
| 153 | + 1, |
| 154 | + builder.makeGetLocal(tempPtr, i32), |
| 155 | + builder.makeGetLocal(tempValue, i32), |
| 156 | + i32)); |
| 157 | + block->list.push_back(builder.makeStore( |
| 158 | + 1, |
| 159 | + curr->offset + 1, |
| 160 | + 1, |
| 161 | + builder.makeGetLocal(tempPtr, i32), |
| 162 | + builder.makeBinary(ShrUInt32, |
| 163 | + builder.makeGetLocal(tempValue, i32), |
| 164 | + builder.makeConst(Literal(int32_t(8)))), |
| 165 | + i32)); |
| 166 | + block->list.push_back(builder.makeStore( |
| 167 | + 1, |
| 168 | + curr->offset + 2, |
| 169 | + 1, |
| 170 | + builder.makeGetLocal(tempPtr, i32), |
| 171 | + builder.makeBinary(ShrUInt32, |
| 172 | + builder.makeGetLocal(tempValue, i32), |
| 173 | + builder.makeConst(Literal(int32_t(16)))), |
| 174 | + i32)); |
| 175 | + block->list.push_back(builder.makeStore( |
| 176 | + 1, |
| 177 | + curr->offset + 3, |
| 178 | + 1, |
| 179 | + builder.makeGetLocal(tempPtr, i32), |
| 180 | + builder.makeBinary(ShrUInt32, |
| 181 | + builder.makeGetLocal(tempValue, i32), |
| 182 | + builder.makeConst(Literal(int32_t(24)))), |
| 183 | + i32)); |
| 184 | + } else if (curr->align == 2) { |
| 185 | + block->list.push_back( |
| 186 | + builder.makeStore(2, |
| 187 | + curr->offset, |
| 188 | + 2, |
| 189 | + builder.makeGetLocal(tempPtr, i32), |
| 190 | + builder.makeGetLocal(tempValue, i32), |
| 191 | + i32)); |
| 192 | + block->list.push_back(builder.makeStore( |
| 193 | + 2, |
| 194 | + curr->offset + 2, |
| 195 | + 2, |
| 196 | + builder.makeGetLocal(tempPtr, i32), |
| 197 | + builder.makeBinary(ShrUInt32, |
| 198 | + builder.makeGetLocal(tempValue, i32), |
| 199 | + builder.makeConst(Literal(int32_t(16)))), |
| 200 | + i32)); |
| 201 | + } else { |
| 202 | + WASM_UNREACHABLE(); |
| 203 | + } |
| 204 | + } else { |
| 205 | + WASM_UNREACHABLE(); |
| 206 | + } |
| 207 | + block->finalize(); |
| 208 | + replaceCurrent(block); |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +Pass* createAlignmentLoweringPass() { return new AlignmentLowering(); } |
| 213 | + |
| 214 | +} // namespace wasm |
0 commit comments